在网页中,div中包含的帖子数量与类名称为“postContainer
”。下面的代码将为每个div
添加一个按钮。jQuery:将附加函数绑定到新添加的div
$('.postContainer').find('.fileText')
.append('<button class="exbutton d1" type="button">postname</button>');
我的代码工作开始,但作为一个新的帖子出现由ajax
,它的DIV没有按钮
在网页中,div中包含的帖子数量与类名称为“postContainer
”。下面的代码将为每个div
添加一个按钮。jQuery:将附加函数绑定到新添加的div
$('.postContainer').find('.fileText')
.append('<button class="exbutton d1" type="button">postname</button>');
我的代码工作开始,但作为一个新的帖子出现由ajax
,它的DIV没有按钮
尝试做这样:https://jsfiddle.net/ff2v0q6v/1/
var AJAXcontent = "Lorem <b>ipsum</b> dolor sit amet..."; // The AJAX post HTML
// OK. now...
var $exbutton = $("<button/>", {
"class": "exbutton d1",
type: "button",
text: "postname"
});
$("<div/>", {
"class": "postContainer",
html: AJAXcontent, // 1. Insert the post content
append: $exbutton, // 2. append the button
appendTo: "body" // 3. append the DIV wherever you want
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
$(document).ajaxComplete(function() {
$("body").find('.postContainer').each(function(){
var $this = $(this);
if($this.find('.exbutton').length === 0) {
$this.append('<button class="exbutton d1" type="button">postname</button>');
}
});
});
欢迎来到Stack Overflow!尽管这段代码可以解决这个问题,但[包括一个解释](// meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)确实有助于提高您的帖子的质量。请记住,您将来会为读者回答问题,而这些人可能不知道您的代码建议的原因。也请尽量不要用解释性注释来挤占代码,这会降低代码和解释的可读性! – Machavity
您可以使用jsfiddle或代码片段ol更好地解释问题? –
'.find()'只是查找当前存在的元素。除非您在事件处理程序中运行它,否则将来无法自动实现此功能。 – Barmar
你能澄清这个问题吗? –