gpt4 book ai didi

javascript - 元素的名称或 ID

转载 作者:行者123 更新时间:2023-11-28 11:40:56 25 4
gpt4 key购买 nike

this.form.sel2类似于document.getElementById('sel2')

我选择的标签如下:

<select size=5 id="sub_player_ids" name="sub[player_ids][]">

当我输入标签名称时,即 sub[player_ids][]在我的 javascript 代码中,出现语法错误。我正在尝试找到一种解决方法,以便我不想使用元素的名称,而是使用 id。

但是使用document.getElementById('sub_player_ids')不工作。

感谢您提供的任何建议。

请参阅下面我的 JavaScript 代码:

      <input type="button" value="--&gt;"
onclick="moveOptions(this.form.sel1, this.form.sel2);" /><br />
<input type="button" value="&lt;--"
onclick="moveOptions(this.form.sel2, this.form.sel1);" />

最佳答案

id您的元素是 sub_player_ids ,不是sel2 。所以document.getElementById("sub_player_ids")会起作用的。

您可能还会发现this.form["sub[player_ids][]"]会工作。使用引用形式可以让您使用在文字形式中无法使用的内容。

需要注意的是,IE7 及更早版本的 getElementById 有一个损坏的版本。这将找到使用给定字符串作为 name 的内容,而不是作为id ,即使页面后面的某些内容实际上将其用作 id 。 (是的,确实如此; more here 。)有些库会为您解决这个问题(例如 jQuery )。实际上,说到库,一个好的库确实可以帮助解决各种浏览器不一致问题,并提供许多方便的实用功能,让您专注于实际的业务问题。您可以考虑查看jQuery , Prototype , YUI , Closure ,或any of several others .

关于javascript - 元素的名称或 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6217207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com