使用参数(JavaScript)将变量直接传递给getElementById

时间:2022-09-01 21:13:19

I'm working on my first JavaScript game and I want to display certain attributes inside of < p> tags. The ID of each < p> tag will be equal to "show-" and the name of the attribute (see example below for the attribute "name").

我正在开发我的第一个JavaScript游戏,我想在

标签内显示某些属性。每个

标签的ID将等于“show-”和属性的名称(请参阅下面的属性“name”的示例)。

But I'm having trouble getting the syntax right for getElementById. Any suggestions?

但是我无法正确获取getElementById的语法。有什么建议?

<p id="show-name"></p>

<script>
    name = "Bob";
    function display(attribute) {
        putItHere = "'show-"+attribute+"'"
        document.getElementById(putItHere).innerHTML = attribute;
    }
    display(name);
</script>

2 个解决方案

#1


You need to target the right element. Currently you are targeting 'show-Bob' and not 'show-name' what your trying to do. So first generate the id, from the key name and then assign a value to that element.

您需要定位正确的元素。目前,您的目标是“show-Bob”,而不是“show-name”您尝试做什么。因此,首先从键名生成id,然后为该元素赋值。

var name = "Bob";
function display(key, value) {
    var putItHere = "show-"+ key;
    document.getElementById(putItHere).innerHTML = value;
}
display('name', name);

note keep in mind that IDs should be unique within the document

请注意,ID应该在文档中是唯一的

However, other way to do that is to target all elements with a specific tag, for instance

但是,其他方法是使用特定标记定位所有元素

<div data-id="name"></div>
<div data-id="name"></div>
<div data-id="name"></div>

<script type="text/javascript">
    var name = "Bob";
    function display(key, value) {
        var putItHere = "show-"+ key;
        var elements = document.querySelectorAll('div[data-id="'+key+'"]');
        for(var eid in elements) {
            var element = elements[eid];
            element.innerHTML = value;
        }
    }
    display('name', name);
</script>

note that that this doesn't work in IE7 and below.

请注意,这在IE7及以下版本中不起作用。

#2


Your attribute is Bob. So putItHere = "'show-"+attribute+"'" is equivalent to: putItHere = "'show-"+"Bob"+"'", which makes no sense.

你的属性是鲍勃。所以putItHere =“'show - ”+ attribute +“'”相当于:putItHere =“'show - ”+“Bob”+“'”,这没有任何意义。

You should get the following error Uncaught TypeError: Cannot set property 'innerHTML' of null.

您应该得到以下错误Uncaught TypeError:无法设置null的属性'innerHTML'。

This should do:

这应该做:

function display(attrname, val){
    document.querySelector('#show-'+attrname).innerText=val;
}

display("name","Bob");

Here is the Fiddle to play with.

这是小提琴。

#1


You need to target the right element. Currently you are targeting 'show-Bob' and not 'show-name' what your trying to do. So first generate the id, from the key name and then assign a value to that element.

您需要定位正确的元素。目前,您的目标是“show-Bob”,而不是“show-name”您尝试做什么。因此,首先从键名生成id,然后为该元素赋值。

var name = "Bob";
function display(key, value) {
    var putItHere = "show-"+ key;
    document.getElementById(putItHere).innerHTML = value;
}
display('name', name);

note keep in mind that IDs should be unique within the document

请注意,ID应该在文档中是唯一的

However, other way to do that is to target all elements with a specific tag, for instance

但是,其他方法是使用特定标记定位所有元素

<div data-id="name"></div>
<div data-id="name"></div>
<div data-id="name"></div>

<script type="text/javascript">
    var name = "Bob";
    function display(key, value) {
        var putItHere = "show-"+ key;
        var elements = document.querySelectorAll('div[data-id="'+key+'"]');
        for(var eid in elements) {
            var element = elements[eid];
            element.innerHTML = value;
        }
    }
    display('name', name);
</script>

note that that this doesn't work in IE7 and below.

请注意,这在IE7及以下版本中不起作用。

#2


Your attribute is Bob. So putItHere = "'show-"+attribute+"'" is equivalent to: putItHere = "'show-"+"Bob"+"'", which makes no sense.

你的属性是鲍勃。所以putItHere =“'show - ”+ attribute +“'”相当于:putItHere =“'show - ”+“Bob”+“'”,这没有任何意义。

You should get the following error Uncaught TypeError: Cannot set property 'innerHTML' of null.

您应该得到以下错误Uncaught TypeError:无法设置null的属性'innerHTML'。

This should do:

这应该做:

function display(attrname, val){
    document.querySelector('#show-'+attrname).innerText=val;
}

display("name","Bob");

Here is the Fiddle to play with.

这是小提琴。