简体   繁体   中英

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:

<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.

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. It is possible with PHP or a server-side language, though.

Or, like @jBot-42 said, you may want to change the id using JS or jQuery using a script, but not like that. 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>

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