简体   繁体   English

.onclick()仅在第一个按钮上触发

[英].onclick() only fires on the first button

When I clicked the "show comments" button, the result always only shown on the first button even though I clicked on the other button (2nd, 3rd, 4th, ... button). 当我单击“显示评论”按钮时,即使我单击了另一个按钮(第二,第三,第四,...按钮),结果也始终仅显示在第一个按钮上。 The content I wanted is shown correctly, but it doesn't displayed on the corresponding button. 我想要的内容已正确显示,但未显示在相应的按钮上。

I already tried to search the solutions over the internet, but I don't think I found the one, especially the other solutions answered with the use of jQuery. 我已经尝试过在Internet上搜索解决方案,但是我认为我找不到一个解决方案,尤其是其他使用jQuery解决的解决方案。 (No jQuery solutions, please) (请没有jQuery解决方案)

HTML: HTML:

        <div id="card-container" class="posts-card-container">
            <p><strong><em>Press button "Show Posts" above (any user)</em></strong></p>
        </div>

some of the script.js: 一些script.js:

function showPosts(data) {
    let cardContainer = document.getElementById('card-container').querySelector('p');
    cardContainer.innerHTML = '';
    for (let i = 0; i < data.length; i++) {
        let cardTitle = '<h3>TITLE: '+data[i].title+'</h3>';
        let cardBody = '<p><em>'+data[i].body+'</em><p>'
        let btnShowComments = '<td><button id="button-show-comment" class="button-comments" postId='+data[i].id
        +' onclick="loadComments('+data[i].id+')">Show Comments</button></td>';
        let cardShowComments = '<div id="show-comments"><p></p></div>';
        let newCard = '<div id="card-container" class="child-card-container">'+cardTitle
                    +cardBody+btnShowComments+cardShowComments+'</div>';
        cardContainer.innerHTML += newCard;
    }
}
function showComments(data) {
    let commentContainer = document.getElementById("show-comments");
    commentContainer.innerHTML = '<h2>Comments</h2>';
    for (let i = 0; i < data.length; i++) {
        let commentPoster = data[i].name+' '+'('+data[i].email+')';
        let commentBody = data[i].body;
        let newComment = '<p><strong>'+commentPoster+'</strong><em> commented: 
                        "'+commentBody+'</em></p>';
        commentContainer.innerHTML += newComment;
    }
}

I expect the newComment is displayed on the corresponding button (eg: if onclick() happened on the third button, newComment must be displayed under the third button), but in my code the newComment always displayed on the first button only. 我希望newComment显示在相应的按钮上(例如:如果onclick()发生在第三个按钮上,则newComment必须显示在第三个按钮下),但是在我的代码中,newComment始终仅显示在第一个按钮上。

The problem here is that querySelector() in showPosts() will only ever give the first element in the set of elements you are looking through that fits the selector. 这里的问题是showPosts()中的querySelector()只会提供要查找的适合选择器的元素集中的第一个元素。

Which isn't what you want, so instead, we can hook up this function to an event, where we put an onclick event surrounding the div containing all buttons, and use event.target to select the button that you are using. 这不是您想要的,因此,我们可以将此函数连接到一个事件,在该事件中,我们在包含所有按钮的div周围放置一个onclick事件,并使用event.target选择要使用的按钮。 So, something like: 因此,类似:

document.getElementById("card-container").onclick=function showPosts(event)
{
    let data=getData();//put in something like this

    //no needed element checking since only buttons have onclick events
    let cardContainer = event.target;
...
}

However, you will have to get data another way in order to use it in the function. 但是,您将不得不以其他方式获取数据才能在函数中使用它。 Not to mention, you shouldn't be using the same ID in two differing elements. 更不用说,您不应在两个不同的元素中使用相同的ID。 IDs are for directing the browser towards a single element. ID用于将浏览器定向到单个元素。

By definition document.getElementById only returns one DOM element, the first one found, even if multiple with the same id are present. 根据定义, document.getElementById仅返回一个DOM元素,即找到的第一个DOM元素,即使存在多个具有相同ID的元素也是如此。 You should find a different way of selecting the button and you should also avoid having multiple elements with the same id as it's intended to be unique. 您应该找到一种不同的选择按钮的方式,并且还应该避免让多个具有相同ID的元素与唯一的元素相同。

Edit: I realized after the fact that that's not exactly what you're asking, but you should still avoid using the same id multiple times, it causes all kinds of problems. 编辑:我意识到这并不是您真正要问的事实,但是您仍然应该避免多次使用相同的ID,否则会引起各种问题。

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

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