切换功能不起作用

Toggle function not working

本文关键字:不起作用 功能      更新时间:2023-09-26

以下是我用于切换的代码。

function toggle_visibility(id) {
       var e = document.getElementById(id);
       if(e.style.display == 'block')
          e.style.display = 'none';
       else
          e.style.display = 'block';
    }

它在我的电脑上运行良好。但是当我在调用.asp页面的服务器端使用它时,它不起作用。我知道我必须使用.live()处理程序。我用jquery滑动切换进行了尝试。但它不起作用。以下是html代码

<div class="issue_button">
<a align="center" href="#" onclick="toggle_visibility('dropdown1');">Link</a></div>
<div id="dropdown1"> <p>Some text</p> </div>

我做错什么了吗?坦率地说,我不知道如何用livehandler编写代码。

既然你说你可以使用jQuery,我建议你使用这个替代代码

function toggle_visibility(id) {
    $('#' + id).toggle();
}

以下是切换的jquery解决方案。

    <div class="issue_button">
        <a align="center" href="#">Link</a>
    </div>
    <div id="dropdown1"> <p>Some text</p> </div>
$(function () {
    $(".issue_button a").click(function () {
        $("#dropdown1").slideToggle();
    });        
});​

http://jsfiddle.net/aJFSy/1/

更新

我不会使用Live,因为它从1.7 开始就被弃用了

代表-http://api.jquery.com/delegate/http://jsfiddle.net/aJFSy/2/

Onhttp://api.jquery.com/on/

try:

function toggle_visibility(id) { 
  var e = document.getElementById(id); 
  if(e.style.display == 'block'){
    e.style.display = 'none'; 
  }else{ 
    e.style.display = 'block';
  }
}