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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:54:58  来源:igfitidea点击:

jquery replace div content html()

jquery

提问by Johnny000

I'm just try to replace the content of a div with new content binded to a click event.

我只是尝试用绑定到点击事件的新内容替换 div 的内容。

JSFIDDLE

JSFIDDLE

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 contentin place of selectorand selectorin place of content, you need to use #with id selctor. you can do it this way

您正在使用content代替selectorselector代替content,您需要#与 id 选择器一起使用。你可以这样做

Live Demo

现场演示

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>