简体   繁体   English

如何编辑在 innerHTML 中创建的元素的样式?

[英]How can i edit the style of elements created in innerHTML?

I wanted to change the style of the second div-element from the innerHTML when i click on the img, but this doesnt work.当我单击 img 时,我想从 innerHTML 更改第二个 div 元素的样式,但这不起作用。

     function display(restaurantArr, name){
     title.innerHTML = `<h2>Available restaurants in ${name}:`
     for(let items of restaurantArr){
       console.log(items)

       main.innerHTML += `
        <div class="container">
            <p class="containerP">${items.restaurant.name}</p>
            <img onclick="editDiv2()" id="imageid" class="imgAdd" src="pngwave.png">
        </div>

        <div id="someid" class="container2">
            <p>Test</p>
            <p>Test</p>
            <p>Test</p>
        </div>
    `

}

} }

The function where i tried to edit the second div.我试图编辑第二个 div 的 function。 Although i understand why this doesnt work, it is a visualisation of what i tried to do:虽然我理解为什么这不起作用,但它是我试图做的事情的可视化:

function editDiv2(){
    const div2 = document.querySelector('.div2')
    div2.style.color = "red";
}
<img onclick="div2()"

should be应该

<img onclick="editDiv2()"

Or the editDiv2 function will not get called.否则不会调用editDiv2 function。

(function() {
    display()      
    clickEvent()    
})(); 

function display(){
        main = document.getElementById("div1")
    main.innerHTML = '<div class="container"><p class="containerP">${items.restaurant.name}</p><img id="imageid" class="imgAdd" src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Image_created_with_a_mobile_phone.png/330px-Image_created_with_a_mobile_phone.png"></div><div id="someid" class="container2"><p>Test</p><p>Test</p><p>Test</p></div>'
}

function clickEvent(){

    var image = document.getElementById("imageid")
        image.addEventListener("click", function(){
    const div2 = document.querySelector('.container2')
    div2.style.color = "red";
})

}

Where ever you call your display function add a new function for your task and call it below display so it can register dom element its click event listner.无论您在哪里调用您的显示 function 为您的任务添加一个新的 function 并在显示下方调用它,以便它可以注册 dom 元素其点击事件列表。

Here is the updated fiddle.这是更新的小提琴。 https://jsfiddle.net/3h61jdfv/ https://jsfiddle.net/3h61jdfv/

It works for me:这个对我有用:

 display([{ restaurant: { name: 'Restaurant Name' } }], 'London') function display(restaurantArr, name) { title.innerHTML = `<h2>Available restaurants in ${name}:` for (let items of restaurantArr) { console.log(items) main.innerHTML += ` <div class="container"> <p class="containerP">${items.restaurant.name}</p> <img onclick="editDiv2()" id="imageid" class="imgAdd" src="pngwave.png"> </div> <div id="someid" class="container2"> <p>Test</p> <p>Test</p> <p>Test</p> </div> ` } } function editDiv2() { const div2 = document.querySelector('#someid') div2.style.color = "red"; }
 <h2 id="title"></h2> <div id="main"></div>

As this is created dynamically you need to add event dynamically to由于这是动态创建的,因此您需要将事件动态添加到

add an ID to the Image为图像添加 ID

<img id="image" class="imgAdd" src="pngwave.png">

then write a click function然后写一个点击 function

const image = document.getElementById("image")

image.addEventListener("click", function(){
    const div2 = document.querySelector('.div2')
    div2.style.color = "red";
})

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM