3

我目前正在开发我们的自制完整的前台 100% javascript CMS,我遇到了很大的问题。用户可以编辑的一些可编辑区域包含在 href 链接中。这些 href 是不可编辑的,然而,当用户点击这些区域(在编辑模式下)时,浏览器会跟随这些链接。

首先,这是一个由 CMS 生成的 html 示例:

<span id ="8a8b8d2e262bde2d01262c08317c000c" class="document">

    <a href="/actions/ecommerce/viderSelectionPalierEtVitrine">
            <img src="/images/logo.gif" id="8a8b8d2e262bde2d01262c08bf83000d" title="" alt="" class="image logo" />
    </a>        
</span>

在这里,例如,用户只能更改 ; 所以我尝试以这种方式管理周围的 href :

        var referenceZone = $(this).attr("id");
    $("#"+documentId+" a").each(function() {
        $(this).click(function() {
            return false; 
        });
    });

referenceZone 是我的周围<span id ="8a8b8d2e262bde2d01262c08317c000c" class="document">

这在我看来是否很棘手?

<**** EDIT ****> 在此处添加了一个用于测试目的的沙箱:http: //jsbin.com/aboke/2

<**** EDIT 2 ****> 我不明白的是 alert(event.type) 甚至没有启动!

//click event disabling on any href of curently edited ${"span.document"}
    $("span#" + documentId + " a").click(function(event) {
              alert(event.type);
              event.preventDefault();
      suppressionZoneModifiable(documentId);
          recupererTexte(referenceZone, documentId);
    });         
4

3 回答 3

3

是的,这是可能的,但不能通过 return 语句。使用 preventDefault();

var referenceZone = $(this).attr("id");
$("#"+documentId+" a").click(function(event) {
    event.preventDefault();
});

此外,除非您对链接执行其他任何操作,否则无需执行 .each() 。但是使用 event.preventDefault()。

请参阅文档的其余部分

于 2010-01-20T13:45:19.940 回答
2

您在http://jsbin.com/aboke/2上的沙箱代码有很多错误。这里有几个:

1.函数参数是一个字符串,但是作为一个数字传递

您应该在此处为参数加上引号:

renduZonesModifiables(8a8b8d2e262bde2d01262c08317c000c);

2.参数与span id不匹配

8a8b8d2e262bde2d01262c08317c000c对比8a8b8d2e262bde2d01262c08bf83000d

3.您在jQuery中使用onclick而不是click

$(this).onclick = function() { return false; }

应该

$(this).click(function(e) {
     e.preventDefault();
});

4.你有一个js错误

“参数列表后缺少 )”(第 81 行)

于 2010-01-20T16:16:20.150 回答
-1
$(this).click(function() {
    return false; 
});

^ 不起作用。Using$(...).click()将该函数添加到它在单击时执行的函数列表中。它不会删除跟随链接的默认行为。

您可以通过执行以下操作覆盖默认链接操作:

this.onclick = function() { return false; }
于 2010-01-20T13:35:55.667 回答