jquery 替换 div 内容 html()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13728358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery replace div content html()
提问by Johnny000
I'm just try to replace the content of a div with new content binded to a click event.
我只是尝试用绑定到点击事件的新内容替换 div 的内容。
When I use appendTo()
instead of of html()
it's working, but I need to clear the old content out so I'm a bit confused why this is not working like it should do
当我使用appendTo()
而不是html()
它正在工作时,但我需要清除旧内容,所以我有点困惑为什么这不能像它应该做的那样工作
回答by Adil
You are using content
in place of selector
and selector
in place of content
, you need to use #
with id selctor. you can do it this way
您正在使用content
代替selector
和selector
代替content
,您需要#
与 id 选择器一起使用。你可以这样做
function showitnow() {
var div_data = "<div ><a href='XXX'>XXX</a></div>";
$("#test1").html(div_data);
}
$("#button1").click(function() {
showitnow();
});
?
回答by Jai
You have some mistakes in your fiddle, That should be like this: http://jsfiddle.net/pCckh/3/
你的小提琴有一些错误,应该是这样的:http: //jsfiddle.net/pCckh/3/
function showitnow() {
var div_data = "<div ><a href='XXX'>XXX</a></div>";
$("#test1").html(div_data);
// ^---selector----^----------your content
}
$("#button1").click(function() {
showitnow();
// ^----------------remove the var and just call the function when click the button
});
回答by Mubin
maybe u can try this..
playing your JQuery as parent and child..
也许你可以试试这个..
作为父母和孩子玩你的JQuery..
<div id="Destination">
<div class="child_content"> item 1 </div>
<div class="child_content"> item 2 </div>
</div>
<div id="Source">
<div class="child_content"> item 1 </div>
<div class="child_content"> item 2 </div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$("#Source .child_content").click(function(){
$(this).parent().remove();
$(this).appendTo("#Destination");
});
});
</script>
Hope this helpfull..
希望这有帮助..
回答by ShreejeetD
Please have a look!Expected result...
请看一看!预期结果...
Only few modification required in same example.
在同一个例子中只需要很少的修改。
<div id = 'div_data'>
<button id="button1">BUTTON1</button>
div>
<div id="test2"></div>