0

我已经做了一些搜索,但找不到答案,所以我想我会在这里问我的第一个问题。

基本上,在单击 div 时,我使用 get 获取一些 html 并附加到 div 的底部。获取的 html 包含最初单击的相同 div。我需要它可以单击以运行与原始 div 相同的功能。这很难解释,并且通过使用 JScrollPane 变得更加复杂

所以这是相关的JavaScript:

$(document).ready(function ($) {
$(document).on('click', 'div#append-tweets', function () {

    //The url is altered to collect the next page of tweets in my example, but this will do
    var currentURL = document.URL;


    currentURL = currentURL + " #content-area > *";

    //gets data from the url
    $.get(currentURL, function (data) {

        //finds the tweets on the page and returns them as HTML
        html = $(data).find('#content-area').html();
        //appends the html to the div - uses api as this is the JScrollPane
        api.getContentPane().append(html);

    }, 'html');

    //reload the scrollbar
    api.reinitialise();

    //makes div button disappear after it is clicked
    document.getElementById('append-tweets').style.display = "none";

});
});

这是相关的 HTML:

<div id="content-area">
//other stuff

    <div id="append-tweets">Load More Tweets</div>

</div>

目前,该功能在第一次单击时运行,并且一切正常。按钮 div 消失(这很好),新内容已加载,包括新按钮 div,但该按钮不可点击,即使它具有相同的 ID。在此先感谢您的帮助,我对 JS 和 JQuery 很陌生。

4

1 回答 1

0

您只能拥有一个具有相同 ID 的元素。选择$("#id")器只会选择具有 ID 的第一个元素#append-tweets,即使有多个元素(同样适用于document.getElementById.

要么使用一个类(.append-tweets),或者如果你必须,我们使用属性选择器(不推荐$("[id=append-tweets]"):。

于 2013-03-05T17:11:21.643 回答