【问题标题】:disable click if of <A> if div selected has same id如果选择的 div 具有相同的 ID,则禁用单击 <A>
【发布时间】:2013-04-03 07:16:15
【问题描述】:

我有一个单页网站,我正在滑动 div,可见的 div 已选择类。

例如,如果 ID ONE 已选择类,则应禁用具有 href #one 的链接。

注意:我有多个可能指向 #one 的 LINKS,所以想全部禁用

<div id="one" class="selected">one</div>
<div id="two">one</div>

<a href="#one" id="linkOne">displays two</a>
<a href="#two" id="linkTwo">displays two</a>

我知道它令人困惑,我真的很抱歉!

编辑:我可以获取 ID 吗?一个'的 div 选择并找到所有具有相同 # '例如#ONE'

【问题讨论】:

    标签: jquery class unbind disable-link


    【解决方案1】:

    这将为所有 A 标签添加一个禁用的类,其中它们的 href 与具有所选类的任何元素的 id 匹配:

    $(function() {
        $("a").removeClass('disabled');
        $('.selected').each(function() {
            $('a[href=#' + $(this).attr('id') + ']').addClass('disabled');
        });
    
    // If by 'disabled', you mean you want the link not to work, then add this:
    
        $('a.disabled').on('click',function(e) {
            alert('you can\'t do that');
            e.preventDefault();
            return false;
        });
    });
    

    示例在这里:http://jsfiddle.net/dtxan/4/

    【讨论】:

      【解决方案2】:

      这样的事情应该可以工作。

      $(".selected").each(function() {
          $('a[href="#' + $(this).attr("id") + '"]').click(function(e) {
              e.preventDefault();
          })
      })
      

      【讨论】:

        【解决方案3】:

        在您的点击回调中,您可以使用属性选择器并选择所有具有匹配 href 的元素。一个简化的例子,因为我不知道你的点击回调是什么样的:

        $(".selectorForAllYourLinks").on("click", function () {
        
            // ### Do what you do to show the div when clicked
        
            // Remove the disabled class from all anchors
            $("a").removeClass("disabled");
        
            // Add the disabled class to all anchors referencing the now visible div
            var id = $(this).attr("href");
            $("a[href='"+id+"']").addClass("disabled");
        });
        

        Live example

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-02-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多