以下是以下html:
<a class="nsg-form--drop-down--label nsg-grad--light-grey nsg-form--drop-down exp-pdp-size-dropdown exp-pdp-dropdown two-column-dropdown selectBox-dropdown" style="" title="" tabindex="0">
<span class="js-selectBox-label">SIZE </span>
<span class="js-selectBox-value nsg-form--drop-down--selected-option"> </span>
</a>
我尝试了以下操作,但没有任何改变。
document.getElementsByClassName("js-selectBox-value nsg-form--drop-down--selected-option").value = '(US 8)'
document.getElementsByClassName("js-selectBox-value nsg-form--drop-down--selected-option").click
document.getElementsByClassName("js-selectBox-value nsg-form--drop-down--selected-option").select
我由于某种原因无法更改该值。当您实际更改该值时,会是这样。
<a class="nsg-form--drop-down--label nsg-grad--light-grey nsg-form--drop-down exp-pdp-size-dropdown exp-pdp-dropdown two-column-dropdown selectBox-dropdown" style="" title="" tabindex="0">
<span class="js-selectBox-label">SIZE </span>
<span class="js-selectBox-value nsg-form--drop-down--selected-option">(US 8)</span>
</a>
感谢您的帮助。
答案 0 :(得分:1)
document.getElementsByClassName
返回htmlcollection。您需要获取索引以更改其中的文本。
document.getElementsByClassName("js-selectBox-value nsg-form--drop-down--selected-option")[0].innerHTML = '(US 8)'
<a class="nsg-form--drop-down--label nsg-grad--light-grey nsg-form--drop-down exp-pdp-size-dropdown exp-pdp-dropdown two-column-dropdown selectBox-dropdown" style="" title="" tabindex="0">
<span class="js-selectBox-label">SIZE </span>
<span class="js-selectBox-value nsg-form--drop-down--selected-option"> </span>
或者,您也可以使用document.querySelector
。它总是以dom中的第一个可用元素为目标
document.querySelector('.nsg-form--drop-down--selected-option')
.innerHTML = '(US-8)'
<a class="nsg-form--drop-down--label nsg-grad--light-grey nsg-form--drop-down exp-pdp-size-dropdown exp-pdp-dropdown two-column-dropdown selectBox-dropdown" style="" title="" tabindex="0">
<span class="js-selectBox-label">SIZE </span>
<span class="js-selectBox-value nsg-form--drop-down--selected-option"> </span>