将 onmouseover 事件分配给动态生成的列表
我正在寻找在 JavaScript 中创建一个函数来操作项目列表。该列表由 CMS 生成,我只能使用 JavaScript 对其进行影响。
让我们假设列表如下:
<ul id="someUniqueID">
<li class="genericClass" id="uniqueGeneratedID"><a href="link1.htm">Link</a></li>
<li class="genericClass" id="uniqueGeneratedID2"><a href="link2.htm">Link 2</a></li>
<li class="genericClass" id="uniqueGeneratedID3"><a href="link3.htm">So on and so forth</a></li>
</ul>
现在,我知道 JavaScript 提供了使用的能力
document.getElementById('uniqueGeneratedID').onmouseover = doSomething();
function doSomething(){ //code here }
,我想做的是获取所有这些 li 并为每个 li 分配一个 onmouseover 函数,而无需专门识别每个 li 元素。
我一直这样做,
var x = document.getElementsByTagName('li');
var changeId = new Array();
for (var i = 0; i < x.length; i++)
{
var j = 0;
changeId[j] = x[i].id;
j++;
}
function mouseOver() {
for(var y = 0; i < x.length; y++){
document.getElementsById(changeId[y]).onmouseover = test(changeId[y]);
}
}
function test(storeContent){
document.getElementsById('storeContent').innerHTML = 'printing' + testId;
}
mouseOver();
问题当然是,这实际上并没有为 ID 生成任何类型的 onmouse over 事件。有没有一种方法可以动态分配所有 li 的 onmouseover 事件,而无需生成
document.getElementsById('uniqueGeneratedID').onmouseover = doEvent();
document.getElementsById('uniqueGeneratedID2').onmouseover = doEvent();
等?
感谢您的任何帮助或建议。
I'm looking to create a function in JavaScript to manipulate a list of items. The list is being generated by a CMS and I can only affect it with JavaScript.
Let's assume the list is as follow:
<ul id="someUniqueID">
<li class="genericClass" id="uniqueGeneratedID"><a href="link1.htm">Link</a></li>
<li class="genericClass" id="uniqueGeneratedID2"><a href="link2.htm">Link 2</a></li>
<li class="genericClass" id="uniqueGeneratedID3"><a href="link3.htm">So on and so forth</a></li>
</ul>
Now, I know that JavaScript offers the ability to use
document.getElementById('uniqueGeneratedID').onmouseover = doSomething();
function doSomething(){ //code here }
What'd I'd like to do is instead grab all of those li's and assign an onmouseover function to each of them without specifically identifying each li element.
I've been doing it like this,
var x = document.getElementsByTagName('li');
var changeId = new Array();
for (var i = 0; i < x.length; i++)
{
var j = 0;
changeId[j] = x[i].id;
j++;
}
function mouseOver() {
for(var y = 0; i < x.length; y++){
document.getElementsById(changeId[y]).onmouseover = test(changeId[y]);
}
}
function test(storeContent){
document.getElementsById('storeContent').innerHTML = 'printing' + testId;
}
mouseOver();
The problem of course is that this doesn't actually generate any kind of onmouse over event for the ID's. Is there a way to dynamically assign all of the li's an onmouseover event without going through generating a
document.getElementsById('uniqueGeneratedID').onmouseover = doEvent();
document.getElementsById('uniqueGeneratedID2').onmouseover = doEvent();
etc.?
Thanks for any help or suggestions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种快速而肮脏的方法是获取父级
并循环其子级:
示例: http://jsfiddle.net/rttXQ/
A quick and dirty way would be to grab the parent
<ul>
and loop through its children:Example: http://jsfiddle.net/rttXQ/