2013-03-26 88 views
0

我有問題隱藏並顯示div元素。 在這種情況下,當用戶點擊顯示尊重內容的年份時。 問題我想要在打開時分別關閉hyperling。 腳本和html是低於 爲此我已嘗試.preventDefault()。但沒有成功。想要使不活動的超鏈接

<script type="text/javascript" > 
      $(document).ready(function() { 
      $("div.new:gt(0)").hide();// to hide all div except for the first one 
      $("div[name=arrow]:eq(0)").hide(); 
      // $("div.nhide:gt(0)").hide(); 
       // $("a[name=new]").hide(); 
      $("a[name=new]").hide(); 
      $('#content a').click(function(selected) { 

       var getID = $(this).attr("id"); 
       var value= $(this).html(); 

       if(value == '&lt;&lt; Hide') 
       { 
        // $("#" + getID + "arrow").hide(); 
        $("a[name=new]").hide(); 
        $("#" + getID + "_info").slideUp('slow'); 
        $("div[name=arrow]").show(); 
        $("div.new").hide(); 
        $(this).hide(); 
        // var getOldId=getID; 
        // $("#" + getID).html('&lt;&lt; Hide').hide(); 
       } 
       if($("a[name=show]")) 
       { 
        // $("div.new:eq(0)").slideUp() 
        $("div.new").hide(); 
        $("div[name=arrow]").show(); 
        $("a[name=new]").hide(); 
        $("#news" + getID + "arrow").hide(); 
        $("#news" + getID + "_info").slideDown(); 

        $("#news" + getID).html('&lt;&lt; Hide').slideDown(); 
       } 

      }); 
     }); 
      </script> 

的HTML代碼如下

<div id="content"> 
<div class="news_year"> 
<a href="#" name="show" id="2012"> 
    <div style="float:left;" name="year" id="news2012year">**2012** </div> 
    <div style="float:left;" name="arrow" id="news2012arrow">&gt;&gt;</div> 
</a> 
    </div> 

<div class="new" id="news2012_info"> 
    <div class="news"> 
     <div class="news_left">News for 2012</div> 
    </div> 
    <div class="nhide" ><a href="#" class="new" name="new" id="news2012">&lt;&lt; Hide</a> </div> 
    </div> 
<div id="content"> 
<div class="news_year"> 
<a href="#" name="show" id="2011"> 
    <div style="float:left;" name="year" id="news2012year">2012 </div> 
    <div style="float:left;" name="arrow" id="news2012arrow">&gt;&gt;</div> 
</a> 
    </div> 

<div class="new" id="news2011_info"> 
    <div class="news"> 
     <div class="news_left">News for 2011</div> 
    </div> 
    <div class="nhide" ><a href="#" class="new" name="new" id="news2011">&lt;&lt; Hide</a> </div> 
    </div> 

Fiddle

回答

0

如果我理解你的問題, event.preventDefault();不適用於所有瀏覽器,所以如果您使用其他瀏覽器,如IE ,則使用event.returnValue = false;而不是that.so你可以檢測到您的瀏覽器使用JavaScript爲

var appname = window.navigator.appName;

0

這是我目前使用在我的項目爲「禁用」的錨標記

禁用主播:

  • 刪除href屬性
  • 更改不透明度爲增加效果

<script> 
$(document).ready(function(){ 
      $("a").click(function() { 
       $(this).fadeTo("fast", .5).removeAttr("href"); 
      }); 
     }); 
</script> 

啓用主播:

$(document).ready(function(){ 
    $("a").click(function() { 
     $(this).fadeIn("fast").attr("href", "http://whatever.com/wherever.html"); 
    }); 
}); 

原代碼,可以發現here

0

環繞聲編碼的點擊功能的內部與if語句中檢查,看是否有變化是真還是假。如果它是錯誤的,它將不會運行代碼,這意味着鏈接實際上處於非活動狀態。試試這個..

var isActive = true; 
if (isActive) { 
    // Your code here 
} 
// The place where you want to de-activate the link 
isActive = false; 

你也可以考慮改變鏈接顏色爲灰色來表示它是無效的。

編輯
只是意識到你想有多個鏈接被禁用..上面的代碼將禁用所有的人。試試下面的代碼(把if周圍的代碼中點擊功能)

if(!$(this).hasClass("disabled")) { 
    // Your code here 
} 

// The place where you want to de-activate the link 
$("#linkid").addClass("disabled"); 

// To re-enable a link 
$("#linkid").removeClass("disabled"); 

// You can even toggle the link from disabled and non-disabled! 
$("#linkid").toggleClass("disabled"); 

然後在你的CSS,你可以有這樣的聲明:

.disabled:link { 
    color:#999; 
} 
0

添加一個名爲類「顯示」,以你的包裝元素擴展你的元素時,並將其隱藏時將其刪除。使用.hasClass('shown')確保不合適的條件從不執行。