简体   繁体   English

我什么时候需要document.getElementByID的引号?

[英]When do I need quotes for document.getElementByID?

I have a simple script which converts a text input of inches into another text input of centimeters. 我有一个简单的脚本,将英寸的文本输入转换为厘米的另一个文本输入。 However, when I pass it to my function, I noticed that I have to use quotes for the argument passed to document.getElementByID. 但是,当我将它传递给我的函数时,我注意到我必须为传递给document.getElementByID的参数使用引号。 I tried newheightcm without quotes and it failed. 我尝试了newheightcm没有引号,但它失败了。 Is there a way to avoid this to maintain consistency for both arguments, ie both have quotes or both have no quotes? 有没有办法避免这种情况来保持两个参数的一致性,即两者都有引号或两者都没有引号?

<script>
function inchestocm(IDin, IDcm) {   
    var inches = IDin.value;
    var centimeters = inches*2.54;  
    document.getElementById(IDcm).value = centimeters;  
}
</script>
<input type="text" id="newheight" onchange="inchestocm(newheight,'newheightcm')">
<input type="text" id="newheightcm">

Because without the quotes you are looking for a variable newheight that is defined in the global scope. 因为没有引号,您正在寻找在全局范围中定义的变量newheight Some browsers do a bad thing and says if I do not have a variable with this id, I look for an DOM element that has that id. 有些浏览器做了一件坏事,并说如果我没有带有此id的变量,我会寻找具有该id的DOM元素。

So what you are passing in is an DOM Element with that id and not a string. 所以你传递的是一个带有id而不是字符串的DOM元素。 That is why IDin.value works. 这就是IDin.value有效的原因。

A better way of doing that is to pass in the scope of the element that was changed. 更好的方法是传入已更改元素的范围。

<input type="text" id="newheight" onchange="inchestocm(this,'newheightcm')">

That way you are not dealing with the browser quirk that made the code run in the first place. 这样你就不会处理使代码首先运行的浏览器怪癖。

function inchestocm(IDin, IDcm) { var inches = IDin.value; function inchestocm(IDin,IDcm){var inches = IDin.value; var centimeters = inches*2.54; var centimeters = inches * 2.54; document.getElementById(IDcm).value = centimeters; document.getElementById(IDcm).value =厘米; } }

newheight is the DOM element <input type="text" id="newheight" onchange="inchestocm(newheight,'newheightcm')"> as there is no variable defined in your code with that name, check epascarello's answer newheight是DOM元素<input type="text" id="newheight" onchange="inchestocm(newheight,'newheightcm')">因为代码中没有定义具有该名称的变量,请检查epascarello的答案

You can checnge your code as 您可以将代码更改为

 <script> function inchestocm(IDin, IDcm) { IDin = document.getElementById(IDin); var inches = IDin.value; var centimeters = inches*2.54; document.getElementById(IDcm).value = centimeters; } </script> <input type="text" id="newheight" onchange="inchestocm('newheight','newheightcm')"> <input type="text" id="newheightcm"> 
<input type="text" id="newheight" onchange="inchestocm(newheight,'newheightcm')">

In your code, both arguments are different, first one is an object from where ur trying to get the value and second one is string where u want to paste the result. 在你的代码中,两个参数都是不同的,第一个是你试图获取值的对象,第二个是你想要粘贴结果的字符串。 Not sure but try this ones. 不确定,但试试这个。

<script>
function inchestocm(IDin, IDcm) {   
var inches = IDin.value;
var centimeters = inches*2.54;  
IDcm.value = centimeters;  
}
</script>
<input type="text" id="newheight" onchange="inchestocm(newheight,document.getElementById('newheightcm'))">
<input type="text" id="newheightcm">

你正在使用函数的参数,这就是为什么你不能使用双引号。如果你使用id的直接名称,那么将使用双引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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