jQuery的追加()不写关闭标签 [英] jquery append() not writing closing tag

查看:121
本文介绍了jQuery的追加()不写关闭标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用谷歌浏览器的jQuery append方法一个奇怪的问题,即它不是写一个结束标记时,该元素为空,但如果将元素包含一个字符。

I'm having an odd issue with jquery append method in Google Chrome where it is not writing a closing tag when the element is empty but will if the element contains a character.

如果我用这个code:

If I use this code:

$('#workZone .canvas').each(function(i) {
        $(this).append('<canvas id="test"></canvas>');
    });
}

我得到这个标记:

I get this in the markup:

<canvas id="test">

如果我用这个code:

If I use this code:

$('#workZone .canvas').each(function(i) {
        $(this).append('<canvas id="test">i</canvas>');
    });
}

在我得到了我所期望的标记来获得:

the I get what I'd expect to get in the markup:

<canvas id="test">i</canvas>

我想结束标记,很明显,但无需包括一次性的性格。这是怎么回事?

I'd like the closing tag, obviously, but without having to include the throwaway character. What is going on?

下面是一个的jsfiddle测试案例: http://jsfiddle.net/YSDnk/

Here is a test case on jsfiddle: http://jsfiddle.net/YSDnk/

谢谢!

推荐答案

试试这个:

$(this).append('<canvas id="test"><!-- //comment --> </canvas>');

您应该把空空格的HTML注释。
通过这种方式,关闭标签将显示。

You should put a html comment in empty spaces. This way the closing tag will show.

这篇关于jQuery的追加()不写关闭标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆