javascript 使用 jQuery 添加新 LI 后如何“刷新” UL?

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

how to "refresh" UL after adding new LI with jQuery?

javascriptjquery

提问by yegor256

I think it's similar to this question, but my problem is about jQuery and HTML5. I'm adding a <LI>element to <UL>and links (<a href="..">) added inside apears as plain text, not clickable. Besides that, images are not visible, while <img src="..">elements are being added propertly.

我认为它类似于这个问题,但我的问题是关于 jQuery 和 HTML5。我正在添加一个<LI>元素<UL>和链接 ( <a href="..">) 作为纯文本添加到 apears 中,不可点击。除此之外,图像是不可见的,而<img src="..">元素正在被适当地添加。

Looks like I need some "refresh" trick to do after using append(). Which one?

看起来我需要在使用append(). 哪一个?

This is my code, just in case:

这是我的代码,以防万一:

$('ul').append($('<li/>').append($('<img/>').attr('src', 'http://example.com')));

The image is not visible, while HTML is being build correctly. I'm trying in Safari 5.1 on Mac.

图像不可见,而 HTML 正在正确构建。我正在 Mac 上尝试使用 Safari 5.1。

BTW, everything works perfectly fine in Firefox 10.0. Maybe the actual problem is that I'm using XML+XSL formatting in Safari and plain HTML5 in Firefox..

顺便说一句,在 Firefox 10.0 中一切正常。也许实际的问题是我在 Safari 中使用 XML+XSL 格式,在 Firefox 中使用纯 HTML5 ..

回答by yegor256

This is how it works instead (see this questionalso):

这就是它的工作原理(另请参阅此问题):

$('#list').html('<li>foo</li>');

回答by Shyju

This will add an li element with an image in it.

这将添加一个带有图像的 li 元素。

   $("ul").append("<li><img src='http://www.yourimage/normal.JPG' /> </li>"); 

Here is the sample : http://jsfiddle.net/xchXQ/1/

这是示例:http: //jsfiddle.net/xchXQ/1/