简体   繁体   English

onclick不起作用<li>

[英]onclick not working on <li>

I am trying to get it so that when a specific <li> is clicked, an image is displayed on the same page. 我正在尝试获取它,以便当单击特定的<li> ,图像会显示在同一页面上。

Below is the code that determines which images to use. 下面是确定使用哪些图像的代码。 It seems to work fine, but I am including it anyway in case it contributes to the problem. 看来工作正常,但无论如何我都会加入它,以防它造成问题。

 <?php
  $j = 0;
  if (file_exists("u/".$_SERVER["REMOTE_ADDR"])) {
    $lines = array_reverse(array_unique(file("u/".$_SERVER["REMOTE_ADDR"])));
    $c = count($lines);
    $c = ($c > 20)?20:$c;
    for ($i = 0; $i < $c; $i++) {
      $lines[$i] = trim($lines[$i]);
      if (file_exists($lines[$i])) {
    echo "<li id=\"item$j\" title=\"".$lines[$i]."\" onclick=\"\"><a id=\"link$j\" href=\"#\">".$lines[$i]."</a></li>";
    $j++;
      }      
    }
   }
 ?>  

The following javascript applies the onclick to each element: 以下javascript将onclick应用于每个元素:

 <script type="text/javascript">
    for (i = 0; i < <?php echo $j; ?>; i++) {
      iLINK = $("item"+i).get("title");
      iHTML = "<img src=\""+iLINK+"\" />";
      $("item"+i).setAttribute("onclick","$('photogal').innerHTML = " +iHTML);
       }
 </script>

When loading the page, the <li> elements are aesthetically fine. 加载页面时, <li>元素在外观上很好。 When inspecting the element I see the following (which appears to be correct): 检查元素时,我看到以下内容(看起来是正确的):

在此处输入图片说明

When changing the last line of javascript to: 将javascript的最后一行更改为:

$("item"+i).setAttribute("onclick","$('photogal').innerHTML = iHTML");

the onclick works as expected, although all links cause the final image to show (because after the loop, iHTML ends up being whatever the last instance of iHTML was). 尽管所有链接都会使最终图像显示出来,但onclick仍然可以正常工作(因为循环之后,iHTML最终将成为iHTML的最后一个实例)。

What is the problem in this code? 这段代码有什么问题?

There are many problems in your code. 您的代码中有很多问题。

  1. Do not use the onclick attribute. 不要使用onclick属性。 Especially do not use javascript to manually write the onclick attribute when you can just assign to the event directly. 当您可以直接分配给事件时,尤其不要使用javascript手动编写onclick属性。
  2. $('photogal').innerHTML = <img...../> is wrong because it is incorrect JS syntax. $('photogal').innerHTML = <img...../>错误,因为它的JS语法不正确。 Try wtiting that line directly into your JS and it will complain about <img.../> not being wrapped in quotes. 尝试直接将该行添加到您的JS中,它会抱怨<img.../>没有用引号引起来。
  3. if the $ is jQuery you dont need to use $('photogal').innerHTML =... you can just do $('photogal').html("<img.../>") 如果$是jQuery,则不需要使用$('photogal').innerHTML =...您只需执行$('photogal').html("<img.../>")
  4. the last thing you mention is because you need a closure in your loop to evaluate the value of i immediately. 您提到的最后一件事是因为您需要在循环中使用闭包来立即评估i的值。 Look up closures to understand what I mean. 查找闭包以了解我的意思。

but I think your immediate problem is #2 above... 但我认为您的直接问题是上述第二名...

so. 所以。 time for an actual MooTools answer (assumes MooTools 1.3+, ideally 1.4.5): 实际的MooTools答案的时间(假设MooTools为1.3+,最好为1.4.5):

this is wrong on many levels: 这在许多层面上都是错误的:

<script type="text/javascript">
    for (i = 0; i < <?php echo $j; ?>; i++) {
      iLINK = $("item"+i).get("title");
      iHTML = "<img src=\""+iLINK+"\" />";
      $("item"+i).setAttribute("onclick","$('photogal').innerHTML = " +iHTML);
       }
</script>

unescaped html as string, invalid event assignment, inline js and so forth. 未转义的html作为字符串,无效的事件分配,内联js等。

you want to assign a single event on the ul or li that contain your lis. 您想在包含您的lis的ul或li上分配一个事件。

if your dom goes like this: 如果您的dom像这样:

<ul class="imageNav">
    <li title="someimage.jpg"><a href='#'>someimage</a></li>
    <li title="someimage.jpg"><a href='#'>someimage</a></li>
    ...
</ul>

<div id="photogal"></div>

then you can have this separate in a single event bind outside of your page. 那么您可以在页面外部的单个事件绑定中单独使用此绑定。

window.addEvent('domready', function() {
    // store the reference to target el
    var photogal = document.id('photogal');

    // delegate a single click event
    document.getElement('ul.imageNav').addEvent('click:relay(li > a)', function(event, el) {
        // stop the event.
        event && event.stop && event.stop();

        // have we got it cached?
        var img = el.retrieve('img');

        // if not, cache image
        if (!img) {            
            el.store('img', img = new Element('img', {
                src: el.getParent('li').get('title') // or el.get('text') -> faster
            }));
        }

        // empty target div and inject new image
        photogal.empty().adopt(img);
    });
});

see it working here: http://jsfiddle.net/dimitar/n6BZD/ 看到它在这里工作: http : //jsfiddle.net/dimitar/n6BZD/

the advantage is, total separation of concerns, DRY. 优点是,完全分离关注点,即DRY。 no need for any element ids or whatever. 无需任何元素ID或任何其他内容。 you can put the src into a more semantic attribute like data-src or even as the href of the a itself so it degrades when no js. 您可以将src置于更具语义性的属性(例如data-src ,甚至可以将其作为a本身的href ,这样在没有js的情况下它会降级。

so here is the semantic solution: http://jsfiddle.net/dimitar/n6BZD/1/ 所以这是语义解决方案: http : //jsfiddle.net/dimitar/n6BZD/1/

have fun 玩得开心

You are essentially saying the following: 您实质上是在说以下内容:

$('photogal').innerHTML = <img src=\""+iLINK+"\" />

This is missing the quotes around your string to set the innerHTML to. 这缺少将内部HTML设置为字符串的引号。

Update: 更新:

$("item"+i).setAttribute("onclick","$('photogal').innerHTML = " +iHTML);

to: 至:

$("item"+i).setAttribute("onclick","$('photogal').innerHTML = '" + iHTML + "';");
$("item"+i).onclick = (function(iHtml) {
    return function() {
       $('photgal').innerHTML = iHTML;
    };
})(iHTML);

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

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