简体   繁体   English

如何从HTML中的JavaScript获取参数

[英]How take parameter from javascript in html

Suppose to have an array: 假设有一个数组:

array: ['hi,'by','miao']

Suppose to pass this array to one html page and I want put the second element in my html code: 假设将此数组传递给一个html页面,我想将第二个元素放在html代码中:

<input type="button" id='{array[1]}'>....

But it doesn't work. 但这是行不通的。 Can anyone help me? 谁能帮我?

嗨,你错过了一个封闭的报价:

array: ['hi','by','miao']

First of all, there's the missing quote, as @erognaut and @unenthusiasticuser have pointed out. 首先,正如@erognaut和@unenthusiasticuser指出的那样,有一个遗漏的报价。

As far as I know, I don't think it's possible to inject JS variables into HTML like you're trying to do there. 据我所知,我认为不可能像您尝试的那样将JS变量注入HTML。 It is possible with PHP or a server-side language, though. 但是,可以使用PHP或服务器端语言。

Or, like @jBot-42 said, you may want to change the id using JS or jQuery using a script, but not like that. 或者,就像@ jBot-42所说的那样,您可能想使用脚本或脚本来使用JS或jQuery更改id ,但并非如此。 For example: 例如:

<input type="button" data-id='array_elem' />

<script>
    var array = ['hi','by','miao'];
    // uses jQuery
    $("input[data-id='array_elem']").attr("id", array[1]);
</script>

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

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