我尝试点击添加,添加的内容将第一次出现。
添加后,我想单击“添加”,它只会切换“已添加”,而不是每次单击时添加“已添加”
Jquery:
$(function(){
var NewContent='<div class="added">Added</div>'
$(".add").click(function(){
$("#spin").after(NewContent);
});
});
HTML:
<span class="add">add</span>
<span id="spin"></span>
这是一个小提琴:http://jsfiddle.net/Abgec/ http://jsfiddle.net/Abgec/
$(function(){
//start with `NewContent` being the HTML to add to the page
var NewContent='<div class="added">Added</div>'
$(".add").click(function(){
//check if `NewContent` is empty or not
if (NewContent != '') {
//if `NewContent` is not empty then add it to the DOM
$("#spin").after(NewContent);
//now that `NewContent` has been added to the DOM, reset it's value to an empty string so this doesn't happen again
NewContent = '';
} else {
//this is not the first click, so just toggle the appearance of the element that has already been added to the DOM
//since we injected the element just after the `#spin` element we can select it relatively to that element by using `.next()`
$('#spin').next().toggle();
}
});
});
这是一个演示:http://jsfiddle.net/7yU3n/ http://jsfiddle.net/7yU3n/
文档用于.toggle()
: http://api.jquery.com/toggle/ http://api.jquery.com/toggle/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)