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

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

4

3 回答 3

4
  1. 查找所有有链接的产品
  2. 将指针作为光标
  3. 处理点击事件改变位置

编码:

$(".product:has(a[href])")
    .css("cursor", "pointer")
    .click(function()
    {
        window.location = $("a", this).attr("href");
    });
于 2010-03-01T16:16:43.450 回答
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;
});
于 2010-03-01T15:56:43.023 回答
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;
            });
        }
    });
});
于 2010-03-01T16:00:27.840 回答