0

我们正在使用一个带有多层 Javascript 选项卡的页面。我们正在尝试根据用户权限禁用对某些选项卡的访问。

例如,这是一个选项卡示例:

<div class="executives tab productA productB productC" runat="server" id="executivesTop">
    <div class="tabLeft">
        <div class="tabMain">
            Executives
        </div>
    </div>
</div>

“tabLeft”和“tabMain”仅用于选项卡的 CSS 布局,不会干扰任何内容。“executivesTop” div 是所有操作发生的地方。我们使用单独的 .js 文件将事件处理程序分配给各个选项卡。

当页面加载时,我想禁用所有选项卡上的所有 mouseenter、mouseleave、mouseover 和 click 事件。我首先在 (window).load 中使用以下行来执行此操作:

//this line disables the event handlers from the tabs
$(".tab").unbind('mouseover mouseenter mouseleave click');
//this line changes the basic css to appear "disabled"
$(".tab").css('color', 'grey');

我们有一个服务器端脚本,它确定当前用户的权限并将其发送到 jQuery 可以读取的字段。jQuery 然后查看这些权限并相应地启用选项卡。

//SubscriptionsInput is a hidden label that has a list of user permissions in the form of "productA,productB,productC"
if (SubscriptionsInput.toLowerCase().indexOf("productA") >= 0) {
    $(".productA").bind('mouseover mouseenter mouseleave click');
}

显然这是行不通的,因为我没有将事件处理程序重新分配给选项卡。各种鼠标事件通过一个类表,点击事件在前面提到的 .js 文件中。我不知道如何重新分配这些。不过,我认为我的推理是合理的,即禁用一切,然后根据权限要求重新启用。

就我所研究的而言,简单的解决方案是使用 .on() 和 .off() 功能,如果我们在 jq1.9 或更高版本,这将是很棒的,但我们在 jq1。 4.2 仍然是,这令人沮丧。我不能使用 .bind()/.unbind() ,除非有一种简单的方法可以从现有的 .js 文件重新附加事件处理程序。

我真的只需要能够禁用和重新启用“鼠标*”和“点击”并且不能使用.on()/.off()。此外,我需要能够将选项卡的 css 恢复为样式表定义。

我希望我已经足够清楚地解释了这一点。任何人都可以提供任何建议或代码技巧,我可以用它来实现这个功能吗?

编辑:我的初始构建加载了所有选项卡,并且权限的缺失将禁用相关选项卡,这将使 .unbind() 想法运作良好(因为权限不会在会话中被授予或删除,所以取消绑定很好),但某些选项卡适用于多个权限级别。我宁愿在用户拥有权限时启用内容,而不是冒险禁用在给定权限级别应该可以访问的东西。

4

1 回答 1

0

将您的逻辑更改为仅在他们没有权限时解除绑定。

//this line disables the event handlers from the tabs
//$(".tab").unbind('mouseover mouseenter mouseleave click');
//this line changes the basic css to appear "disabled"
//$(".tab").css('color', 'grey');

//SubscriptionsInput is a hidden label that has a list of user permissions in the form of "productA,productB,productC"
if (SubscriptionsInput.toLowerCase().indexOf("productA") == 0) {
    $(".productA").unbind('mouseover mouseenter mouseleave click').css('color', 'grey');
}
于 2014-04-09T18:19:30.247 回答