jquery .text() 或 .html() 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4204318/
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 16:49:17  来源:igfitidea点击:

jquery .text() or .html() not working

jquery

提问by Vaibhav Gupta

I am dynamically generating a div and want to change its content via html()/text(), but its not working.

我正在动态生成一个 div 并希望通过 html()/text() 更改其内容,但它不起作用。

Here's the code:

这是代码:

var div = '<div class="ps_album"><img alt="" src="images/1px.gif" class="thmb"><div class="ps_desc"><h2>title</h2></div></div>';

$(div).find('.ps_desc').html('<h1>Hello</h1>');
$(div).appendTo('body');

in above code, the 3rd line i.e.

在上面的代码中,第三行即

$(div).find('.ps_desc').html('<h1>Hello</h1>');

not working.. why??

不工作..为什么?

回答by Pointy

The problem is that you're re-evaluating $(div). The first line that operates on the not-yet-added DOM fragment does not update the string value of "div". Thus, the second time you create $(div)you start back over with the original fragment!

问题是您正在重新评估$(div). 对尚未添加的 DOM 片段进行操作的第一行不会更新 "div" 的字符串值。因此,第二次创建时,$(div)您将从原始片段重新开始!

Try this:

尝试这个:

 $(div).find('.ps_desc').html('<h1>Hello World</h1>').end().appendTo($('body'));

The jQuery .end()method "pops" the effect of the preceding .find()call, so in this case it gets you back to the original point of navigation, which is the fragment you're building from the string. The subsequent call to .appendTo()therefore will act on the outer <div>you've built, and so that shoulddo the right thing.

jQuery.end()方法“弹出”前面.find()调用的效果,因此在这种情况下,它使您返回到导航的原始点,即您从字符串构建的片段。因此,随后的调用.appendTo()将作用于<div>您构建的外部,因此应该做正确的事情。

回答by xil3

Try this:

尝试这个:

$(div).appendTo('body');
$('body').find('.ps_desc').html('<h1>Hello</h1>');

回答by Pavel Nikolov

$(div).appendTo('body');
$('.ps_desc').html('<h1>Hello</h1>');

UPDATE:

更新:

A working demo: http://jsfiddle.net/nbF2H/4/

一个工作演示:http: //jsfiddle.net/nbF2H/4/

回答by tbleckert

$('body').append('<div class="ps_album"><img alt="" src="images/1px.gif" class="thmb"><div class="ps_desc"><h2>title</h2></div></div>')
         .find('.ps_desc')
         .html('<h1>Hello World</h1>');

See here

看这里

回答by vad

Today I am presenting on improvements to security note templates

今天我介绍 improvements to security note templates

This is an example of XSS vulnerability :

这是一个例子 XSS vulnerability :

$(div).appendTo('body');
$("body div").find('.ps_desc').html('<h1></h1>');

回答by live-love

Make sure you are accessing the correct id.

确保您正在访问正确的 ID。

If you are using ASP.NET, it could be because ASP.NET changes the id names, if you add "runat="server".

如果您使用的是 ASP.NET,这可能是因为 ASP.NET 更改了 id 名称,如果您添加了“runat="server”。

Try accessing it by using the class name instead and see if it works.

尝试使用类名访问它,看看它是否有效。

    <td id="mytd" class="myclass" runat="server"></td>
    $('.myclass').html()
    $('.myclass').text()

回答by Chinmayee G

You should reverse the order i.e.

你应该颠倒顺序即

$(div).appendTo('body');
$("body div").find('.ps_desc').html('<h1>Hello</h1>');

find and html methods works on elements inside DOM. Unless you append your HTML string to DOM these jquery methods will not work.

find 和 html 方法适用于 DOM 内的元素。除非您将 HTML 字符串附加到 DOM,否则这些 jquery 方法将不起作用。

Option II

选项二

var div = $('<div class="ps_album"><img alt="" src="images/1px.gif" class="thmb"><div class="ps_desc"><h2>title</h2></div></div>');

$(div).appendTo('body');
$(div).find('.ps_desc').html('<h1>Hello</h1>');