[英]Add CSS attribute to certain span that contain a CSS style using JavaScript
我有類似的東西
<span style="font-size: 1.7px; letter-spacing: -1.7px; color: #edf6fc">text</span>
和
<span style="font-size: 99.81%; color: #080007">text </span>
我如何選擇第一個並添加; 讓我們說display: none
到它的樣式(或刪除那個span)但不是第二個使用JavaScript。
或者如何刪除具有CSS屬性letter-spacing
所有范圍?
為這些元素提供class
或id
。
.spanOne{
display: none;
}
要么
動態創建這些元素並將它們存儲在變量中。 然后,您可以使用這些變量來添加或刪除屬性。
var spanOne = //code to create span element;
var spanTwo = //code to create another span element;
$(spanOne).hide();
要么
$('#parentElementID').children('span').eq(0).hide();
更新 :
$('div').children('span').filter(function () {
return this.style.fontSize == "17.7px"; //in your case 1.7px
}).eq(0).css('backgroundColor', 'red');
PS:我建議在該元素中添加一個類名或id。
一種簡單的方法是添加class
名或id
:
<span id="iHateYou" style="font-size: 1.7px; letter-spacing: -1.7px; color: #edf6fc">text</span>
<span style="font-size: 99.81%; color: #080007">text </span>
和js:
document.getElementById('iHateYou').remove();
例如在Firefox中使用類似Firebug的添加,右鍵單擊問題標簽,例如<span>
然后選擇inspect元素。 轉到代碼並找到具有唯一標識符(ID)的父代碼,選擇該父代表並通過父代子選擇添加CSS。
#abc span{display: none;}
在樣式表中添加它。 這將使唯一標識符的子項附加您的樣式並覆蓋應用於它的樣式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.