简体   繁体   English

JQuery - 如何在一些html中添加单个html标签?

[英]JQuery - How to add a single html tag to some html?

I want to insert just a single <span> before the text below (right before LINK): 我想在下面的文本之前插入一个<span> (在LINK之前):

<li><a href="">LINK</a></li>

So, the above becomes 所以,上面变成了

<li><a href=""><span>LINK</a></li>

Here is my JQuery Code: 这是我的JQuery代码:

$('#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? 当我在firebug中查看代码之后,我看到<span></span> ,我如何获得开始span标记而不是结束标记?

Firstly, what you want is malformed HTML. 首先,你想要的是格式错误的HTML。 jQuery is going to make it hard if not impossible to create that. jQuery即使不是不可能创建它也会变得困难。 I would suggest doing it in a single step rather than multiple. 我建议只用一步而不是多步。 Try: 尝试:

$("li > a").each(function() {
  $(this).contents().wrapAll("<span>");
});

Input: 输入:

<ul>
  <li><a href="...">testing</a></li>
  <li><a href="...">rich <b>content</b> with <i>inner tags</i></a></li>
</ul>

Output: 输出:

<ul>
  <li><a href="..."><span>testing</span></a></li>
  <li><a href="..."><span>rich <b>content</b> with <i>inner tags</i></span></a></li> 
</ul>

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. 我遇到了一个问题,我正在使用第三方API,它使用php curl将平面HTML放入我的网页。 In the api HTML that was returned was some malformed span tags that were just killing my rendering in IE. 在api HTML中返回的是一些格式错误的span标签,它们只是在IE中删除了我的渲染。 I used Jquery's replaceWith() function to fix the issue. 我使用Jquery的replaceWith()函数来解决问题。 Heres an example: 下面是一个例子:

Malformed HTML:(notice the unopened span tag) 格式错误的HTML :(注意未打开的span标记)

<a class="aa_float_right aa_navlink" href="http:whatever">My Account</a></span> 

My solution: 我的解决方案

$('.aa_navlink').replaceWith('<span class="test"><a class="aa_float_right aa_navlink" href="http:whatever">My Account</a>');

Resulting HTML: 产生的HTML:

<span class="test"><a class="aa_float_right aa_navlink" href="http:whatever">My Account</a></span>

BOOM we now have well formed HTML no thanks so crappy 3rd party API's (and I mean really crappy). BOOM我们现在已经很好地形成了HTML,不用谢谢这么糟糕的第三方API(我的意思是真的很糟糕)。

This fix was given to me from the developers at SaltWater Co. SaltWater公司的开发人员给了我这个修复程序

I don't know why you want it but it can be done this way, 我不知道你为什么要它,但它可以这样做,

$('#mainnav li a').html(function(i,v){
   return '<span>' + v;
})

When you use prepend , you're not adding markup text to the page, you're adding elements to the page. 当您使用prepend ,您不会向页面添加标记文本,而是向页面添加元素 Elements don't have "start" and "end" tags, elements are elements. 元素没有“开始”和“结束”标记,元素是元素。 If you want to wrap a bunch of elements in a span (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. 如果你想在一个span包装一堆元素(我猜这是你想要的,如果你想在一个地方插入它的开头而在另一个地方插入它的结尾),你需要添加跨度到容器然后将元素移入其中。

For example, this moves all of the links out of the element foo and moves them into a span inside container : 例如,这会将所有链接移出元素foo并将它们移动到container内的span container

var stuff;
var container;

stuff = $('#foo a');
span = $("<span style='background-color: yellow'>");
span.append(stuff);
$('#container').append(span);

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. 许多jQuery方法被设计为仅从半写标签自动创建元素,例如,对于.prepend(), "<span>"将导致整个span元素被添加到DOM。 Or, as you'll see it in Firebug, "<span></span>" inserted firstly in the anchor element. 或者,正如您在Firebug中看到的那样, "<span></span>"首先插入到锚元素中。

I don't see why you'd want to create invalid HTML, but if you must, you can do it this way: 我不明白你为什么要创建无效的HTML,但如果必须,你可以这样做:

$("li > a").html("<span>"+$("li > a").html());

Or, as Reigel suggests, with an anonymous function to the .html() method (I wrote my answer rather quickly and, obviously, unthoughtfully): 或者,正如Reigel建议的那样,使用.html()方法的匿名函数(我很快就写了我的答案,很明显,非常有意思):

$("li > a").html(function (idx, html) {
    return "<span>" + html;
});

Otherwise, it could seem that you want to wrap the contents of the anchor element in a span. 否则,您似乎想要在span中包装anchor元素的内容。 In that case, you should instead use one of the jQuery wrapping methods : 在这种情况下,您应该使用其中一个jQuery 包装 方法

$("li > a").wrap("<span>");

You're not meant to create both the starting element and the closing element yourself (when using jQuery). 你不是要自己创建起始元素和结束元素(使用jQuery时)。

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

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