简体   繁体   中英

how to change the value of <span lang> in javascript?

i have an element in html as shown below.

<tr><td class="HELPTEXT"><span lang="HLPMTXT1" id="HLPMTXT1"></span></td></tr>

i want to change the value of lang according to particular condition. I tried as given below.but its not working.

<script> document.getElementById("HLPMTXT1").lang ="HLPMTXT2" </script>

Could anyone help me for changing the value of lang attribute of span?

document.getElementById('HLPMTXT1').setAttribute('lang', 'HLPMTXT2');

并非所有属性都可以通过对象属性访问

You should use setAttribute(name, value) to do that, so your code would look like:

document.getElementById("HLPMTXT1").setAttribute("lang", "HLPMTXT2");

You can also use getAttribute(name) to retrieve the value using JavaScript.

  1. https://developer.mozilla.org/en/DOM/element.setAttribute
  2. https://developer.mozilla.org/en/DOM/element.getAttribute

Edit: It's also possible that your script is not working because you're trying to access the element before it exists in the DOM. Best way to insure that your element exists is by either: a) putting your script tag after the element, b) using the unload event to delay execution of your JS until everything is loaded, or c) use the DOMContentLoaded event. The latter, however, is a bit tricky to get to work cross-browser (without reusing somebody else's code that already addresses those problems) so you might want to read up on it first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM