简体   繁体   English

避免jQuery追加结束标签

[英]Avoid jquery append closing tag

How can I ensure that this: 我如何确保这一点:

$('.graph svg').append('<polyline points="' + '3,' +point+ ' 97,' +point+ '"style="fill:none;stroke:#FFFFFF;stroke-width:0.5"/>');

Actually results in a self closing tag ..../> 实际上会导致一个自闭标签..../>

Rather than closing with </polyline> 而不是使用</polyline>关闭

For some reason only the former renders on iOS. 由于某些原因,只有前者会在iOS上渲染。

It doesn't result in any tags at all; 它根本不会产生任何标签 it results in elements in the DOM. 它导致DOM中的元素。 Tags are textual means of describing elements. 标签是描述元素的文字方式。 When you give that string to jQuery, it asks the browser to parse that string and create elements (objects) in memory. 当您将该字符串提供给jQuery时,它会要求浏览器解析该字符串并在内存中创建元素(对象)。 The only tags involved are the ones you give to jQuery. 唯一涉及的标签是您给jQuery的标签。


From your update (comment): 根据您的更新(评论):

...is there another way of doing this that avoids the append method? ...还有另一种避免append方法的方法吗? Here's a fiddle that refuses to work on iOS http://jsfiddle.net/rCfrF/23 这是一个拒绝在iOS上使用的小提琴http://jsfiddle.net/rCfrF/23

That doesn't work for me on Chrome, Firefox, or IE either. 在Chrome,Firefox或IE上,这都不适合我。 I don't think you can add to SVG elements like that, I think jQuery tries to create an HTML element polyline rather than the SVG polyline (which is namespaced). 我认为您不能像这样添加SVG元素,我认为jQuery尝试创建HTML元素polyline而不是SVG polyline (已命名空间)。

This works on Chrome, Firefox, IE, and my iPhone 5: Updated version of your fiddle on JSBin (jsFiddle doesn't work properly on my iPhone 5) 在Chrome,Firefox,IE和我的iPhone 5 均可使用: 在JSBin上更新了小提琴的版本 (jsFiddle在我的iPhone 5上无法正常工作)

function clickappend() {
    var svg = $("#graph svg")[0];
    var polyline = svg.ownerDocument.createElementNS('http://www.w3.org/2000/svg', 'polyline');
    polyline.setAttribute("points", "20,0 20,100");
    polyline.style.fill = "none";
    polyline.style.stroke = "#232323";
    polyline.style.strokeWidth = "0.5";
    svg.appendChild(polyline);
    alert('ran');
}

您可以使用:

$('.graph svg').html($('.graph svg').html() + '<polyline points="' + '3,' +point+ ' 97,' +point+ '"style="fill:none;stroke:#FFFFFF;stroke-width:0.5"/>');

This is the inspector's problem. 这是检查员的问题。 There is a substantial difference between void elements (aka self-closing elements) and others, in that they cannot accept descendant nodes. void元素 (又称自动关闭元素)与其他元素之间存在实质性差异,因为它们不能接受后代节点。 polyline is such a void element. polyline就是这样的void元素。 The inspector may show it as having a closing tag, but it shouldn't be able to accept methods such as – if you tried to insert content between its opening and closing tags that content would likely be inserted after it in the DOM. 检查器可能会显示它具有结束标记,但是它不应该接受诸如–如果您尝试在其开始标记和结束标记之间插入内容,而该内容很可能会在DOM中插入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM