在div上使用JavaScript函数单击使用get获取的div
我做了一些搜索,找不到答案,所以我想我会在这里问我的第一个问题 基本上,在单击一个div时,我使用get获取一些html并附加到div的底部。获取的html包括最初单击的同一个div。我需要它能够被点击以运行与原始div相同的函数。这很难解释,并且由于使用JScrollPane而变得更加复杂 这就是相关的JavaScript:在div上使用JavaScript函数单击使用get获取的div,javascript,jquery,Javascript,Jquery,我做了一些搜索,找不到答案,所以我想我会在这里问我的第一个问题 基本上,在单击一个div时,我使用get获取一些html并附加到div的底部。获取的html包括最初单击的同一个div。我需要它能够被点击以运行与原始div相同的函数。这很难解释,并且由于使用JScrollPane而变得更加复杂 这就是相关的JavaScript: $(document).ready(function ($) { $(document).on('click', 'div#append-tweets', functio
$(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>
//其他东西
加载更多推文
目前,该函数在第一次单击时运行,一切正常。button div消失(这很好),新内容被加载,包括新的button div,但该按钮无法单击,即使它具有相同的ID。提前感谢您的帮助,我对JS和JQuery非常陌生。您只能有一个具有相同ID的元素。
$(“\35; ID”)
选择器将仅选择具有#append tweets
ID的第一个元素,即使有多个元素(同样适用于document.getElementById
)
使用类(
.append tweets
),或者如果必须,使用属性选择器(不推荐使用():$(“[id=append tweets]”)
“即使它具有相同的id”.那可能是你的问题吧?给新的项目新的ID并适当地更改代码。是的,谢谢。令人尴尬的是,这已经解决了。我发誓我一小时前就试着把它改成课堂了!谢谢你的帮助。