在div上使用JavaScript函数单击已使用get获取的div

时间:2013-03-05 17:06:51

标签: javascript jquery

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

基本上,在点击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都很陌生。

1 个答案:

答案 0 :(得分:0)

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

要么使用课程(.append-tweets),要么必须,请使用属性选择器(不推荐):$("[id=append-tweets]")。< / p>