2013-02-27 53 views
0

有以下HTML,使用JQuery我想刪除售價:我如何刪除它,我用下面的jQuery的嘗試,但它不工作刪除售價:從字體標籤

jQuery代碼:

$('Sale Price:').detach(); 

HTML代碼:

<b class="cartvalB"> 
    <font class="pricecolor colors_productprice"> 
    Sale Price: 
    <span class="cartvalB_span" itemprop="price">$175.00</span> 
    </font> 
    </b> 
+3

2013年'font'元素? – undefined 2013-02-27 12:52:41

回答

-1
<script> 
alert("HI..."); 
$(document).ready(function(){ 
alert($('span')[0].innerHTML); 
$('span')[0].innerHTML = ""; 
alert($('span')[0].innerHTML); 
}); 
</script> 

</head> 

<body> 
<b class="cartvalB"> 
    <font class="pricecolor colors_productprice"> 
    <span>Sale Price:</span> 
    <span class="cartvalB_span" itemprop="price">$175.00</span> 
    </font> 
    </b> 
</body> 
+0

當你有'html()'客棧jquery時,爲什麼'innerHTML'? – mithunsatheesh 2013-02-27 13:01:00

-1
var span = $('.cartvalB .cartvalB_span'); 
$('.cartvalB font').empty().append(span); 

應該做的伎倆

+1

不確定是否可以從他的最終結果標記中刪除'font'標記。我可能會選擇更安全的路線,並且將空字體標籤(也可以通過班級定位)而不是完整的父跨度。 – 2013-02-27 13:05:38

+0

@EliGassert哦忘了'font'標籤:\ – shevski 2013-02-27 13:07:10

0

這可以通過更改標記一點,並使用JavaScript來實現。我相信你也可以用jQuery來做到這一點,但我並不是那麼熟悉。首先,你需要把你的文本放在一個可以被javascript訪問的元素中。

<div id="myDiv" class="cartvalB"> 
    Sale Price: 
</div> 

如果你在ASP.Net中這樣做,你可以使用標籤控件。

現在,你要訪問的DIV在javascript:

function myScript(){ 
    var myDiv = document.getElementById('myDiv') 
    myDiv.innerText ='' 
} 

這改變了你的div的內部文本。您還可以更改可見性,樣式等。Divs就像所有的html元素一樣,具有一組可通過DOM訪問的屬性。檢查他們在這裏:http://msdn.microsoft.com/en-us/library/ie/ms535240%28v=vs.85%29.aspx

我只是自由交付這個,所以它沒有測試,但應該與一點點推動工作。如果沒有,請告訴我。

+0

如果改變標記是一個選項,他甚至不需要jquery刪除元素 - 他可以直接從文檔中排除它,或者在它上面放上一個類,然後在樣式表中設置'display:none'。所以我的預感正在改變,標記不是他的選擇。值得發現,但! – 2013-02-27 13:04:04

0

你不能在這樣的文本上查詢。您必須改爲選擇父元素,循環內容,然後查找(並刪除)所需的文本節點。參考How do I select text nodes with jQuery?Remove text with jQuery

例子:

var textContent = 'textContent' in document.body ? 'textContent' : 'innerText'; 

$('.colors_productprice').contents().filter(function() 
{ 
    if(this.nodeType == 3) 
    { 
    var content = this[textContent]; 

    if(content == 'Sale Price:') return true; // found just this text node... now remove it 
    } 

    return false; 
}).remove(); 

或者,您可以處理內部HTML,如:

var html = $('.colors_productprice').html(); 
html = html.replace('Sale Price:', ''); 
$('.colors_productprice').html(html); 

這可能會產生的後果,如果您有綁定到任何子元素的任何事件的colors_productprice,但如果不是,那麼這應該是一個安全和簡單的解決方案。