可点击的块指针

时间:2010-03-01 15:54:52

标签: jquery css pointers cursor clickable

// clickable blocks
$(".product").click(
function () {
    window.location = $(this).find('a').attr("href").css("cursor", "pointer");
    return false;
});

容器可点击但光标保持不变。为什么css选择器不工作?

3 个答案:

答案 0 :(得分:4)

  1. 查找所有具有链接的产品
  2. 将指针设为光标
  3. 处理点击事件以更改位置
  4. 代码:

    $(".product:has(a[href])")
        .css("cursor", "pointer")
        .click(function()
        {
            window.location = $("a", this).attr("href");
        });
    

答案 1 :(得分:2)

“.attr”的返回值是属性值,而不是jquery对象。

$(".product").click(function () {
  window.location = $(this).find('a').attr("href");
  $(this).find('a').css("cursor", "pointer");
  return false;
});

如果你想让“容器”有一个新光标,那么你可能想要这个:

$(".product").click(function () {
  window.location = $(this).find('a').attr("href");
  $(this).css("cursor", "pointer");
  return false;
});

答案 2 :(得分:0)

你真的想在点击块中设置光标吗?在我看来,要做你真正想要的事情,你需要这样做:

编辑:好的,考虑到你只想在那些包含a:

的事件上设置点击事件
$(function() { // or $(document).ready(function() {
    $(".product").each(function() {
        if ($(this).has('a')) {
            $(this).css("cursor", "pointer");
            $(this).click(
            function () {
                window.location = $(this).find('a').attr("href");
                return false;
            });
        }
    });
});