簡體   English   中英

替換href標記內的跨度文本

[英]Replacing span text within href tag

誰能幫我,我正在嘗試更改href標簽內的span標簽的文本。 以下代碼似乎可以在Jsfiddle中使用,但不能在我的網站上使用?

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');
    }); 
});

我對div和類使用了類似的方法,它們在Jsfiddle和我的網站中都可以正常工作! 任何其他建議,我可以實現這一目標?

您必須在<a> -element內部選擇跨度:

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

您可能沒有正確將類添加到<a>元素中。

您的jQuery代碼正在尋找$("a.productdesc") ,我建議將第一行代碼更改為$(function() {

順便說一句,您的代碼缺少.find() ,您可以這樣使用:

$(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');
    }); 
});

您必須使用兒童metdod,因此請嘗試此操作;

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

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

您的代碼僅是正確的,請嘗試放入$('a[href^="#pdpTab1"]').addClass('productdesc'); $(function(){});

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

如果使用.find('span')則可以保留跨度,否則跨度將消失。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM