Won*_*nka 4 jquery append toggle
我正在尝试点击添加,添加的内容将首次出现.
添加之后,我想点击添加,它只会切换"添加",而不是在每次点击时添加"添加"
jQuery的:
$(function(){
var NewContent='<div class="added">Added</div>'
$(".add").click(function(){
$("#spin").after(NewContent);
});
});
Run Code Online (Sandbox Code Playgroud)
HTML:
<span class="add">add</span>
<span id="spin"></span>
Run Code Online (Sandbox Code Playgroud)
这是一个小提琴: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();
}
});
});
Run Code Online (Sandbox Code Playgroud)
这是一个演示:http://jsfiddle.net/7yU3n/
文档.toggle()
:http://api.jquery.com/toggle/
归档时间: |
|
查看次数: |
14596 次 |
最近记录: |