javascript 将 document.getElementById 存储在变量中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29614210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:47:04  来源:igfitidea点击:

Storing document.getElementById in a variable?

javascripthtmldocumentgetelementbyid

提问by Bramt

I have this script that is supposed to change the text of the button when the button is clicked.

我有这个脚本应该在单击按钮时更改按钮的文本。

<body>
   <button onclick="toggleText(this);" id="id">Edit</button>
</body>

function toggleText(element){
    var text = document.getElementById(element.id).textContent;

    if (text == 'Edit') {
        text = 'Done';
    } else {
        text = 'Edit';
    }
}

But it doesn't work. It only works when you put document.getElementById(element.id).textContentdirectly into the if statements.

但它不起作用。它仅在您document.getElementById(element.id).textContent直接放入 if 语句时才有效。

How do I get the variable to store properly?

如何让变量正确存储?

回答by Sami Kuhmonen

Since you already get the element, you don't need to get it again. You can just use element.

由于您已经获得了该元素,因此您无需再次获得它。你可以只使用元素。

But the reason why you can't change it is that you're only changing the variable that contains the text. It does not point to the element's properties. You need to use this:

但是你不能改变它的原因是你只改变了包含文本的变量。它不指向元素的属性。你需要使用这个:

function toggleText(element){
  var text = element.textContent;

  if (text == 'Edit') {
    element.textContent = 'Done';
  } else {
    element.textContent = 'Edit';
  }
}

回答by Omar Elawady

When you access document.getElementById(element.id).textContentyou get the value of it, not the reference. So changes to it won't affect the element.

当您访问时,document.getElementById(element.id).textContent您获得的是它的价值,而不是参考。所以对它的更改不会影响元素。

But when you assign element to variable, it gets reference to it.

但是当您将元素分配给变量时,它会得到对它的引用。

var element = document.getElementById(element.id);
if (element.textContent == 'Edit') {
        element.textContent = 'Done';
    } else {
        element.textContent = 'Edit';
}

回答by srikanth t

Just use:

只需使用:

index.js

索引.js

var mainText = document.getElementById("mainText").value;
document.write(mainText);

index.html

索引.html

<textarea id="mainText"></textarea>

回答by Moishe Lipsker

As tymeJV commented, you can store by reference the element you get by id. Its property is stored by value. Instead store the element in a variable and access its property from the variable.

正如 tymeJV 所评论的,您可以通过引用存储您通过 id 获得的元素。它的属性是按值存储的。而是将元素存储在变量中并从变量访问其属性。

var text = document.getElementById(element.id);

if (text.textContent == 'Edit') {
     text.textContent = 'Done';
} else {
    text.textContent = 'Edit';
}

回答by Cyril CHAPON

Is jquery an option ?

jquery 是一个选项吗?

I used to do something like :

我曾经做过类似的事情:

var a = $('#theid');
A.something