当没有id或类时,使用javascript更改标签值

当没有id或类时,使用javascript更改标签值,javascript,selector,Javascript,Selector,你能帮我用javascript更改值吗,但代码没有id或类这里是代码: <label for="flights-origin-prepop-whitelabel_en">Origin</label> 原点 我想用不同的词来改变“起源” 我的代码的一些示例: <button role="flights_submit" type="submit">Search</button> <div class="mewtwo-flights-desti

你能帮我用javascript更改值吗,但代码没有id或类这里是代码:

<label for="flights-origin-prepop-whitelabel_en">Origin</label>
原点
我想用不同的词来改变“起源”

我的代码的一些示例:

<button role="flights_submit" type="submit">Search</button>
<div class="mewtwo-flights-destination">
<label for="flights-destination-prepop-whitelabel_en">Destination</label>
<input type="text" 
       name="destination_name" 
       autocomplete="off" required="" 
       id="flights-destination-prepop-whitelabel_en" 
       placeholder="Destination" 
       data-label="Destination" 
       role="flights-destination" 
       data-modal-modifier="whitelabel_en" 
       data-placeholder-initialized="true">

<input type="hidden" 
       name="destination_iata" 
       id="flights-destination-whitelabel_en" value="">
</div>
搜索
目的地
如何更改此占位符“目的地”和标签文本目的地


谢谢

是的,当然,您可以使用带有标签选择器的
查询选择器
,包括用于数据的标签选择器

console.log(document.querySelector(“label[for='flights-destination-prepp-whitelabel\u en']))
搜索
目的地

您可以更改原始文本以执行以下操作:

document.querySelector("label[for='flights-origin-prepop-whitelabel_en']").textContent = 'New Text';

您可以使用“属性选择器”(attribute selector)查找
for
属性及其特定值,找到正确的元素

var btn=document.querySelector(“按钮”);
btn.addEventListener(“单击”,函数(){
var el=document.querySelector(“[for=flights-destination-prepp-whitelabel_en]”);
el.textContent=“新值”;
var输入=document.getElementById(“flights-destination-prepp-whitelabel_en”);
setAttribute(“占位符”、“新值”);
});
搜索
目的地

第一步是设置要定位的元素的数组,在本例中是
标签。然后使用
for
循环遍历该数组,查看
标签的
textContent
是否是您想要更改的内容。最后,一旦“钩住”了所需的标签,就可以根据需要更改其属性。还请注意,
textContent
方法不兼容跨浏览器,因此我加入了一个三元脚本,这要归功于

HTML:

搜索


目的地

其他标签
JavaScript:

//https://stackoverflow.com/a/285608/5076162
//https://stackoverflow.com/a/13506703/5076162
var labelArray=document.getElementsByTagName('LABEL');
控制台日志(labelArray);
var textToChange='Destination';
对于(变量i=0;i

//https://stackoverflow.com/a/285608/5076162
//https://stackoverflow.com/a/13506703/5076162
var labelArray=document.getElementsByTagName('LABEL');
控制台日志(labelArray);
var textToChange='Destination';
对于(变量i=0;i
搜索


目的地

其他标签
同时删除标签选择器部分会稍微快一点。所以:document.querySelector(“[for='flights-destination-prepp-whitelabel_en'])很容易被捕获,但在我的网站上不起作用的是link:booking.aviabiletebi.org我把它复制到了控制台,它可以工作
document.querySelector('label[for=“flights-destination-prepp-whitelabel_en”])
(在你的网页上)也可以稍微加快标签选择器部分的放置速度。所以:document.querySelector(“[for='flights-destination-prepp-whitelabel_en']),不要使用
.innerHTML
当你不传递任何HTML时,请使用
textContent
。很抱歉,它对我不起作用这里是链接:在JSFIDLE上它起作用,但在我的网站上对我不起作用。只需打开javascript控制台并键入document.querySelector(“[for='flights-origin-prepp-whitelabel_en']”);它将返回标签。
<button role="flights_submit" type="submit">Search</button>
<br/>
<br/>
<div class="mewtwo-flights-destination">
    <label for="flights-destination-prepop-whitelabel_en">Destination</label>

    <input type="text" name="destination_name" autocomplete="off" required="" id="flights-destination-prepop-whitelabel_en" placeholder="Destination" data-label="Destination" role="flights-destination" data-modal-modifier="whitelabel_en" data-placeholder-initialized="true">
    <br/>
    <br/>
    <label for="somethingElse">OtherLabel</label>
    <input type="text" id='somethingElse'>

    <input type="hidden" name="destination_iata" id="somethingElse" value="">
</div>
//https://stackoverflow.com/a/285608/5076162
//https://stackoverflow.com/a/13506703/5076162

var labelArray = document.getElementsByTagName('LABEL');
console.log(labelArray);
var textToChange = 'Destination';

for (var i = 0; i < labelArray.length; i++) {
    var currentLabel = labelArray[i]
    var text = ('innerText' in labelArray[i]) ? 'innerText' : 'textContent';
    if (currentLabel[text] === 'Destination') {
        var matchingInput = document.getElementById(currentLabel.htmlFor);
        var newText = 'changedDestination';
        var newPlaceHolder = 'changedPlaceHolder';

        currentLabel[text] = newText;
        matchingInput.placeholder = newPlaceHolder;
    }
}