将 onmouseover 事件分配给动态生成的列表

发布于 2024-12-09 06:17:19 字数 1537 浏览 1 评论 0原文

我正在寻找在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

So尛奶瓶 2024-12-16 06:17:19

一种快速而肮脏的方法是获取父级

    并循环其子级:

var ul = document.getElementById("someUniqueID"),
    lis = ul.getElementsByTagName("li");

for (var i = 0, l = lis.length; i < l; i++) {
    lis[i].onmouseover = function() {
        this.style.color = "red";
    };
}

示例: http://jsfiddle.net/rttXQ/

A quick and dirty way would be to grab the parent <ul> and loop through its children:

var ul = document.getElementById("someUniqueID"),
    lis = ul.getElementsByTagName("li");

for (var i = 0, l = lis.length; i < l; i++) {
    lis[i].onmouseover = function() {
        this.style.color = "red";
    };
}

Example: http://jsfiddle.net/rttXQ/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文