0

I know this is pretty easy, and I am probably missing something very obvious, but here it goes.

I have an <ul> with a bunch of stuff inside.

I have set jquery, so that when <ul> is clicked, to assign it a class name, but it doesn't work.

$(document).ready(function() {
    $('#list').click(function(event) {
        event.stopPropagation();
        $(this).addClass('left');
    });
});

It works however, if the <ul> is wider than the stuff in it, and I click where there is no stuff.

Here is the HTML

 <ul id="list">
        <li>
          <a href="#" class="pos1">
            <span class="ei_preview"></span>
            <span class="ei_image"></span>
          </a>
          <div class="ei_descr">
            <h2>Name</h2>
            <h3>Last Name</h3>
            <p>
           Herp derp derp, hurr durr
            </p>
            <p>
           Herp derp derp, hurr durr
            </p>
          </div>
        </li>
        <li>
          <a href="#" class="pos2">
            <span class="ei_preview"></span>
            <span class="ei_image"></span>
          </a>
          <div class="ei_descr">
            <h2>Name</h2>
            <h3>Last name</h3>
            <p>
           Herp derp derp, hurr durr
            </p>
            <p>
            Herp derp derp, hurr durr
            </p>
          </div>

        </li>
</ul>

EDIT: After looking at the CSS, when I click a <li>, it doesn't work for the <ul> because <li> is set as position: relative;. Any idea how to overcome this without removing the position property ?

CSS as requested : http://hastebin.com/pahaxexabo.css

The .left is the class I want to assign to the <ul>.

4

1 回答 1

1

那么没有html,很难调试你的javascript。

查看您的 java 脚本,当单击具有 id 'list' 的元素时,您的函数将触发。

您的 ul 标签是否具有属性 id="list" ?

如果要单击任何 ul 标记来触发事件,请将 jquery 中的第二行更改为以下内容:

...
$('ul').click(function(event) {
...

另外,我不确定你为什么需要下面的代码,看起来它是多余的

event.stopPropagation();

你的javascript似乎很好。问题可能出在您的 CSS 上。尝试使用已添加的类编写 HTML,并检查 ul 是否具有所需的样式。一旦它具有所需的 css 样式,然后再次尝试使用 jquery。

于 2013-02-10T23:12:00.137 回答