Javascript:更改元素html的值
如何使用javascript更改第一个价格区间的值Javascript:更改元素html的值,javascript,html,Javascript,Html,如何使用javascript更改第一个价格区间的值 <span class="regular-price" id="product-price-27"> <span class="price">$2,699.99</span> </span> <span class="price">$2,000</span> var parentElement = document.getElementsByClassNa
<span class="regular-price" id="product-price-27">
<span class="price">$2,699.99</span>
</span>
<span class="price">$2,000</span>
var parentElement = document.getElementsByClassName("price")[0]
parentElement.innerText="your price"
$2,699.99
$2,000
我尝试过使用getElementByClassName,但对于这两个,它发生了变化
我只想更改第一个元素的值
$2699.99
然后使用getElementsByClassName(“price”)[0]
它只适用于第一个元素试试这个,首先获取外部span元素,然后使用该类获取其中的所有元素
var outerSpan = document.getElementById("product-price-27");
var innerSpans = outerSpan.getElementsByClassName("price");
innerSpans[0].innerHTML = "$xxxx";
这是一个
请看看这个
使用javascript
<span class="regular-price" id="product-price-27">
<span class="price">$2,699.99</span>
</span>
<span class="price">$2,000</span>
var parentElement = document.getElementsByClassName("price")[0]
parentElement.innerText="your price"
请尝试getElementById(“product-price-27”)检查此项
getElementsByClassName(“price”)
-获取其类名price
[0]
-它是价格
类的第一个元素
JavaScript
HTML
$2,699.99
$2,000
如果是代码上带有类price
的第一个元素:
var myelement=document.getElementsByClassName('price')[0];
myelement.innerHTML='new content';
如果您不确定它是否是第一个,但您知道实际值,您可以:
var actualValue='whatever';
var elements=document.getElementsByClassName('price');
for (var i in elements){
if (elements[i].innerHTML==actualValue)
{
elements[i].innerHTML=newValue;
break;
}
}
给你,检查一下房间
var x = document.getElementsByClassName("price");
x[0].innerHTML = "$2000";
http://jsfiddle.net/kushalraid/mjVH8/
OP没有提到jqueryWell,如果它有效的话也没关系,但是“.price”不会找到它为什么?它不是?
。price
是一种jquery格式,用于查找class=“price”
,与#price
查找id=“price”
的方式相同。对香草JS来说不是这样的
var x = document.getElementsByClassName("price");
x[0].innerHTML = "$2000";
http://jsfiddle.net/kushalraid/mjVH8/