使用Jquery在标记内附加标记

时间:2021-04-16 20:26:47

I am trying to append span tag inside .block-leftnav ul li .parent a, so the tag gets added only after the .parent a but what jquery does is, it adds the span tag after every a href tag on my page.

我试图在.block-leftnav ul li .parent a中附加span标签,所以标签只在.parent a之后添加,但jquery的作用是,它在我的页面上的每个href标签之后添加span标签。

Below is the code I am using to acheive what I want:

下面是我用来实现我想要的代码:

jQuery(document).ready(function() {
    jQuery('.block-leftnav ul li .parent > a').append('<span class="glyphicon arrow"></span>');
});

It was a simple fix, I figured it out myself and also updated the above code. Just had to add the ">" before the "a" to target the anchor tags of that particular div.

这是一个简单的修复,我自己想出来并且还更新了上面的代码。只需在“a”之前添加“>”以定位该特定div的锚标签。

Thanks Guys for your prompt response.

感谢大家的快速回复。

4 个解决方案

#1


0  

$(document).ready(function(){
   $('.block-leftnav ul li .parent a').prepend('<span class="glyphicon arrow"></span>');
});

#2


0  

one thing you can do it like this..

有一件事你可以这样做..

 $(function(){
              $("#my_id").append("<span>sdfsdfd</span>");
            })

give the id to your anchor tag

将id添加到您的锚标签

 <a id="my_id" href="sdsd.xyz">xyz</a>

This is the code which is working fine for me

这个代码对我来说很好

#3


0  

It sounds like you want to append an arrow character inside any a element that has the class parent.

听起来你想在任何一个具有父类的元素中附加一个箭头字符。

To do so, your selector needs to be a.parent, not .parent a. The former will search for a elements with the class parent while the latter will search for any a element inside an element with the class parent.

要做到这一点,你的选择器需要是a.parent,而不是.parent a。前者将使用类父级搜索元素,而后者将使用类父级搜索元素内的任何元素。

$(function() {
    $('.block-leftnav ul li a.parent')
        .append('<span class="glyphicon arrow"></span>');
});

If you want to find the a element inside a li that has class parent, use the same concept, but apply it to the li:

如果要在具有类父级的li中找到a元素,请使用相同的概念,但将其应用于li:

$(function() {
    $('.block-leftnav ul li.parent a')
        .append('<span class="glyphicon arrow"></span>');
});

#4


0  

try this

http://jsfiddle.net/bpf6t3Lw/1/

html

<a href="#"></a>

js

$(document).ready(function(){
    $('a').append("<span>Appended item</span>");
});

#1


0  

$(document).ready(function(){
   $('.block-leftnav ul li .parent a').prepend('<span class="glyphicon arrow"></span>');
});

#2


0  

one thing you can do it like this..

有一件事你可以这样做..

 $(function(){
              $("#my_id").append("<span>sdfsdfd</span>");
            })

give the id to your anchor tag

将id添加到您的锚标签

 <a id="my_id" href="sdsd.xyz">xyz</a>

This is the code which is working fine for me

这个代码对我来说很好

#3


0  

It sounds like you want to append an arrow character inside any a element that has the class parent.

听起来你想在任何一个具有父类的元素中附加一个箭头字符。

To do so, your selector needs to be a.parent, not .parent a. The former will search for a elements with the class parent while the latter will search for any a element inside an element with the class parent.

要做到这一点,你的选择器需要是a.parent,而不是.parent a。前者将使用类父级搜索元素,而后者将使用类父级搜索元素内的任何元素。

$(function() {
    $('.block-leftnav ul li a.parent')
        .append('<span class="glyphicon arrow"></span>');
});

If you want to find the a element inside a li that has class parent, use the same concept, but apply it to the li:

如果要在具有类父级的li中找到a元素,请使用相同的概念,但将其应用于li:

$(function() {
    $('.block-leftnav ul li.parent a')
        .append('<span class="glyphicon arrow"></span>');
});

#4


0  

try this

http://jsfiddle.net/bpf6t3Lw/1/

html

<a href="#"></a>

js

$(document).ready(function(){
    $('a').append("<span>Appended item</span>");
});