jquery append()不写结束标签 [英] jquery append() not writing closing tag

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

问题描述

我在 Google Chrome 中的 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.

如果我使用此代码:

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

我在标记中得到了这个:

I get this in the markup:

<canvas id="test">

如果我使用此代码:

$('#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 append()不写结束标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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