JQuery - 如何向某些 html 添加单个 html 标签?
我想在下面的文本之前(就在 LINK 之前)插入一个 :
<li><a href="">LINK</a></li>
因此,上面的内容变成
<li><a href=""><span>LINK</a></li>
这是我的 JQuery 代码:
$('#mainnav li a').prepend('<span>');
当我查看在 firebug 中编写代码后,我看到 ,如何获取开始跨度标签而不是结束标签?
I want to insert just a single <span> before the text below (right before LINK):
<li><a href="">LINK</a></li>
So, the above becomes
<li><a href=""><span>LINK</a></li>
Here is my JQuery Code:
$('#mainnav li a').prepend('<span>');
When I view the code in firebug after, I see <span></span>
, how do I just get the opening span tag and not the closing tag?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先,您想要的是格式错误的 HTML。 jQuery 将使创建它变得困难甚至不可能。我建议一步完成,而不是多个步骤。尝试:
输入:
输出:
Firstly, what you want is malformed HTML. jQuery is going to make it hard if not impossible to create that. I would suggest doing it in a single step rather than multiple. Try:
Input:
Output:
我遇到了一个问题,我正在使用第 3 方 API,该 API 使用 php curl 将平面 HTML 放入我的网页中。在返回的 api HTML 中,有一些格式错误的 span 标签,这些标签导致我在 IE 中的渲染失效。我使用Jquery 的replaceWith() 函数来解决这个问题。下面是一个例子:
格式错误的 HTML:(注意未打开的 span 标签)
我的解决方案:
结果 HTML:
BOOM 我们现在已经有了格式良好的 HTML,不,谢谢,这么蹩脚的第 3 方 API(我的意思是真的很蹩脚)。
此修复是 SaltWater Co. 的开发人员提供给我的
I ran into an issue where I was using a 3rd party API that was using php curl to place flat HTML into my webpage. In the api HTML that was returned was some malformed span tags that were just killing my rendering in IE. I used Jquery's replaceWith() function to fix the issue. Heres an example:
Malformed HTML:(notice the unopened span tag)
My solution:
Resulting HTML:
BOOM we now have well formed HTML no thanks so crappy 3rd party API's (and I mean really crappy).
This fix was given to me from the developers at SaltWater Co.
我不知道你为什么想要它,但可以这样做,
I don't know why you want it but it can be done this way,
当您使用
prepend
时,您并不是向页面添加标记文本,而是向页面添加元素。元素没有“开始”和“结束”标签,元素就是元素。如果你想将一堆元素包装在一个span
中(我猜这就是你想要的,如果你想将它的开头插入一个地方,将它的结尾插入另一个地方),您需要将跨度添加到容器中,然后将元素移入其中。例如,这会将所有链接移出元素
foo
并将它们移动到container
内的跨度中:When you use
prepend
, you're not adding markup text to the page, you're adding elements to the page. Elements don't have "start" and "end" tags, elements are elements. If you want to wrap a bunch of elements in aspan
(I'm guessing that's what you want, if you want to insert the beginning of it in one place and the end of it in another), you need to add the span to the container and then move the elements into it.For example, this moves all of the links out of the element
foo
and moves them into a span insidecontainer
:许多 jQuery 方法被设计为仅从半写标签自动创建元素,例如 .prepend() 的
""
将导致将整个 span 元素添加到 DOM。或者,正如您在 Firebug 中看到的那样,""
首先插入到锚元素中。我不明白为什么你想要创建无效的 HTML,但如果你必须,你可以这样做:
或者,正如 Reigel 建议的那样,使用 .html() 方法的匿名函数(我写了我的答案而不是快速且明显地不假思索):
否则,您可能会想将锚元素的内容包装在一个跨度中。在这种情况下,您应该使用 jQuery 包装 方法 >:
您不应该自己创建起始元素和结束元素(使用 jQuery 时)。
Many jQuery methods are designed to automatically create elements from only half-written tags, like,
"<span>"
for a .prepend() will result in an entire span element being added to the DOM. Or, as you'll see it in Firebug,"<span></span>"
inserted firstly in the anchor element.I don't see why you'd want to create invalid HTML, but if you must, you can do it this way:
Or, as Reigel suggests, with an anonymous function to the .html() method (I wrote my answer rather quickly and, obviously, unthoughtfully):
Otherwise, it could seem that you want to wrap the contents of the anchor element in a span. In that case, you should instead use one of the jQuery wrapping methods:
You're not meant to create both the starting element and the closing element yourself (when using jQuery).