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

时间:2016-11-10 20:09:01

标签: javascript selector

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

<label for="flights-origin-prepop-whitelabel_en">Origin</label>

我想改变&#34; Origin&#34;用不同的词。

我的代码的一些例子:

<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>

如何更改此占位符&#34;目的地&#34;和标签文字目的地?

由于

4 个答案:

答案 0 :(得分:2)

您可以更改原点texto执行此操作:

<ContentView.GestureRecognizers>
    <TapGestureRecognizer Tapped="OnWidgetTapped" />
</ContentView.GestureRecognizers>

答案 1 :(得分:0)

是的,当然,您可以将console.log(document.querySelector("label[for='flights-destination-prepop-whitelabel_en']"))与标签选择器一起使用,包括数据

&#13;
&#13;
<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>
&#13;
.caption-clr {
  background-color: rgba(0, 0, 0, .1);
}
body {
  background-color: lightgreen;
}
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您可以使用&#34;属性选择器找到正确的元素&#34;查找for属性及其特定值。

&#13;
&#13;
var btn = document.querySelector("button");
btn.addEventListener("click", function(){
  var el = document.querySelector("[for=flights-destination-prepop-whitelabel_en]");
  el.textContent = "New Value";
  
  var input = document.getElementById("flights-destination-prepop-whitelabel_en");
  input.setAttribute("placeholder","New Value");

  
});
&#13;
<button role="flights_submit" type="button">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>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

第一步是设置要定位的元素数组,在本例中为label。然后,您将使用for循环遍历该数组,以查看textContent的{​​{1}}是否是您想要更改的内容。最后,一旦你迷上了“#39;您需要的标签,您可以根据需要更改它的属性。另请注意,label方法不符合浏览器标准,因此我提供了一个三元脚本,可以避免this handy StackOverflow post

  

HTML:

textContent
  

JavaScript的:

<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>
  

jsfiddle Example

&#13;
&#13;
//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;
    }
}
&#13;
//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;
  }
}
&#13;
&#13;
&#13;