简体   繁体   English

如何使用基于使用JavaScript单击的特定按钮的数组中的信息替换div上的信息?

[英]How can i replace information on a div with information from an array based on specific button being clicked using JavaScript?

So this is my first question being asked here and i don't know if I'm just overthinking this or overlooking something..... 所以这是我在这里被问到的第一个问题,我不知道我是不是只是在思考这个问题或忽略了什么......

I'm trying to make a script that will replace the information inside a pop-up on my website with information from an array depending on what item of my catalog that opens the actual pop-up is being clicked using JavaScript. 我正在尝试制作一个脚本,用我的网站上的弹出窗口中的信息替换来自数组的信息,具体取决于打开实际弹出窗口的哪个项目使用JavaScript进行点击。

I want to mention that I'm not well versed in programming. 我想提一下,我不太熟悉编程。 I can handle and understand the logic part of the problem. 我可以处理和理解问题的逻辑部分。 I know what i want, but I'm stuck trying to code it out. 我知道我想要什么,但我一直在努力编写代码。

So I currently have only 1 item in my catalog that I'm using for testing and all I'm trying to do at the moment is change the information inside the p tags: 所以我目前在我的目录中只有一个项目用于测试,而我现在要做的就是更改p标签内的信息:

p id="item-description" target="0"> Lorem ipsum dolor sit amet < p p id =“item-description”target =“0”> Lorem ipsum dolor sit amet <p

Now... in my JavaScript file I've stored an array.... "let description = ["description 1", "description 2", "description 3"] 现在......在我的JavaScript文件中,我存储了一个数组....“let description = [”description 1“,”description 2“,”description 3“]

I've also stored the value of the attribute "target" for the item : "let target = document.getElementById('item-description'); target = target.getAttribute("target"); " 我还为项目存储了属性“target”的值:“let target = document.getElementById('item-description'); target = target.getAttribute(”target“);”

(once i can do it for a single one, I'm going to be changing functionality to store the value of the attribute "target" for whichever item is going to be clicked) (一旦我可以为一个人做,我将改变功能,以存储属性“目标”的值,无论要点击哪个项目)

This is how I'm trying to approach this ..... once i store the value of "target" i need compare it to every position value in the array until they both match (this is where I'm currently stuck) . 这就是我试图接近这个......一旦我存储了“目标”的值,我需要将它与数组中的每个位置值进行比较,直到它们都匹配(这是我目前卡住的地方)。 Once there is a match found, that match value will be stored and it will determine what content from the array i want to use to replace the description in my pop up.. 找到匹配项后,将存储该匹配值,它将确定我要用于替换弹出窗口中的描述的数组中的内容。

The biggest problem that I'm facing right now is actually storing the value of the position of the element in the array to compare it to the "target" clicked on or the sample one i currently have as "0". 我现在面临的最大问题实际上是存储元素在数组中的位置值,以将其与点击的“目标”或我当前拥有的样本“0”进行比较。

How can this be approached? 怎么能接近这个? or is there a better way I'm overlooking that can help me accomplish my main task? 还是有一种更好的方式我可以帮助我完成我的主要任务?

Not totally sure I understand your question, but I think you're trying to do something like this: 我不完全确定我理解你的问题,但我认为你正在尝试这样做:

const myArrayOfStuff = ["stuff1", "stuff2", "stuff3"];
const target = document.getElementById("item-description");
const targetAtt = target.getAttribute("target");
target.innerHTML = myArrayOfStuff[targetAtt]

That will replace the text in the div "item-description" with "stuff1" if the target is set to 0. 如果目标设置为0,那将用“stuff1”替换div“item-description”中的文本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用javascript在特定按钮下方显示信息 - how to display information underneath a specific button using javascript 如何将特定按钮链接到 div 内容可以根据单击哪个按钮而变化? - How do I link specific buttons to a div which content can vary based on which button is clicked? 如何更改我单击的特定按钮的此按钮背景颜色? 使用 javascript - How can I change this button background color of the specific button that i have clicked? using javascript 如何使用javascript从div获取信息到另一个div - How to get information from divs to another div using javascript 在Javascript中单击后,如何从用户列表中获取特定用户信息? - How can I get the specific user information from the list of users upon click in Javascript? 使用jQuery,如何将信息存储在动态创建的div中? - Using jQuery, how can I store information in a dynamically created div? 单击带有DOM的按钮时卸载信息-Javascript - Unload information when clicked in button with DOM - Javascript 如何在 Javascript 中使用工厂传递数组信息? - How do I pass array information using a factory in Javascript? 如何将ajax的响应定位到单击的特定div - How can I target the response from an ajax to a specific div that was clicked 如何使用javascript从同一工作表的2个不同的excel单元中提取信息? 一世 - How can I pull information from 2 different excel cells on the same worksheet using javascript? I
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM