简体   繁体   中英

Replacing span text within href tag

can anyone help me i'm trying to change the text of a span tag, within a href tag. The following code seems to work in Jsfiddle but not on my site?

http://jsfiddle.net/cqQt4/

HTML:

<a href="#pdpTab1"><span>Product Description</span></a>

JavaScript:

$('a[href^="#pdpTab1"]').addClass('productdesc');
$(function() {
    $("a.productdesc").each(function(index,el){
        $(el).text('Descripción del producto');
    }); 
});

I have used similar method for div and a class an they work perfectly both in Jsfiddle and my site! Any suggestions on other ways i can achieve this?

您必须在<a> -element内部选择跨度:

$(el).find('span').text('Descripción del producto');

You probably are not adding the class to the <a> element correctly.

Your jQuery code is looking for this $("a.productdesc") , I suggest changing the first line of code to inside the $(function() { .

Btw your code is missing a .find() , you can use like this:

$(function() {
    $('a[href^="#pdpTab1"]').addClass('productdesc'); // you should add this class to the html instead.
    $("a.productdesc").each(function(index,el){
        $(el).find('span').text('Descripción del producto');
    }); 
});

you must use children metdod,so try this;

var aTag=$('a href["#pdpTab1"]');

$(aTag).each(function(incoming,index)
{
$(this).children('span').text('something');
});

your code is correct only, try putting $('a[href^="#pdpTab1"]').addClass('productdesc'); inside $(function(){});

  $(function() {
    $('a[href^="#pdpTab1"]').addClass('productdesc');
    $("a.productdesc").each(function(index,el){
     $(el).text('Descripción del producto');
   }); 
 });

you can keep the span if you use .find('span') else the span will be gone.

http://jsfiddle.net/cqQt4/1/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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