简体   繁体   English

正确访问<ul>

[英]properly accessing images within a <ul>

I currently have: 我目前有:

<div id="thumbImages">
        <ul>
            <li><img src="thumbimages/test1.jpg" alt="thumb1" width="125" height="100" /></li>
            <li><img src="thumbimages/test2.jpg" alt="thumb2" width="125" height="100" /></li>
            <li><img src="thumbimages/test3.jpg" alt="thumb3" width="125" height="100" /></li>
            <li><img src="thumbimages/test4.jpg" alt="thumb4" width="125" height="100" /></li>
        </ul>
    </div>

in my HTML 在我的HTML中

and I am attempting to add button like functionality to the thumbnails with this javascript 并且我正在尝试使用此javascript将类似按钮的功能添加到缩略图

var isMousedOver = [
false,
false,
false,
false
];

function init()
{
   DoStuffWithThumbs();
}

this.onload = init();

function DoStuffWithThumbs()
{
   var thumbs = document.getElementById("thumbImages");
   var itemsUL = thumbs.getElementsByTagName("ul");
   var itemsLI = itemsUL.item(0).getElementsByTagName("li");
   for (var i = 0; i < itemsLI.length; ++i)
   { 
      var curThumb = itemsLI[i];
      curThumb.onclick = DoStuff(i);
      curThumb.onmouseover = MouseOver(i);
      curThumb.onmouseout = MouseOut(i);
   }
}

function MouseOver(val)
{
   isMousedOver[val] = true;
}

function MouseOut(val)
{
   isMousedOver[val] = false;
}

function DoStuff(val)
{
   if(isMousedOver[val] == true)
   {
       //stuff is done here ( I know the stuff in question is working)
   }
}

However currently I am getting no visible response from this at all on the page when I have separately tested the result itself ( simply flipping an image and changing some text on the page based on another array). 但是,当我单独测试结果本身时(仅翻转图像并基于另一个数组更改页面上的某些文本),当前在页面上根本没有任何可见的响应。 Which leads me to believe I am accessing the elements incorrectly. 这使我相信我在错误地访问元素。 I am new to using Javascript alongside html so forgive me if I have made some grave error. 我是初次使用HTML和HTML的Javascript,所以如果我犯了一些严重的错误,请原谅我。 Am I accessing my elements properly? 我是否可以正确访问元素? or is this entirely the wrong way to go about accessing them/using onmouseover/onmouseout? 还是这完全是错误的方式来访问它们/使用onmouseover / onmouseout?

You are invoking functions instead of assigning them as handlers. 您正在调用函数,而不是将其分配为处理程序。 So you need to fix that first. 因此,您需要先解决该问题。 To fix it according to your current approach, each function you invoke would need to return a function that references the current i value. 要根据您当前的方法进行修复,您调用的每个函数都需要返回一个引用当前i值的函数。

But instead of tracking the mouseover state in an Array, it would be simpler to track it on the DOM element itself by adding a property. 但是,与其在Array中跟踪鼠标悬停状态,不如通过添加属性在DOM元素本身上跟踪它。

function DoStuffWithThumbs() {
   var thumbs = document.getElementById("thumbImages"),
       itemsUL = thumbs.getElementsByTagName("ul"),
       itemsLI = itemsUL.item(0).getElementsByTagName("li");

   for (var i = 0; i < itemsLI.length; ++i) { 
      itemsLI[i].onclick = DoStuff;
      itemsLI[i].onmouseover = MouseOver;
      itemsLI[i].onmouseout = MouseOut;
   }
}

function MouseOver(val) {
   this._over = true;
}

function MouseOut(val) {
   this._over = false;
}

function DoStuff(val) {
   if(this._over === true) {
       // do your stuff
   }
}

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

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