需要重新启用onclick事件中禁用的onmouseout

时间:2012-01-05 19:48:08

标签: javascript javascript-events onmouseout

我在这里和互联网上搜索了可以重新启用onClick代码中已禁用的onMouseOut的代码。我发现了一些东西,但在我的情况下没有任何作用。

在页面上有一个容器div,它包含两个div,一个用于图像,另一个用于相应的文本。在图像下面是四个链接(1,2,3,4),以无序列表排列。当访问者滚过#2时,图像需要更改为img2,匹配的文本div需要从“隐藏”变为“显示”。 onMouseOut将图像和文本重置为原始版本。 onClick将图像/文本更改为单击的#,然后禁用onMouseOut。

现在我需要一种方法让onMOuseOut在下次访问者浏览#link时再次运行。正如我现在所做的那样,一旦onMouseOut被禁用,它将一直处于禁用状态,直到我重新加载页面。

以下是目前的代码:

<head>
<script>
if (document.images) {
book1 = new Image
    book2 = new Image
    book3 = new Image
    book4 = new Image

book1.src = "/llb/assets/book1.jpg"
    book2.src = "/llb/assets/book2.jpg"
book3.src = "/llb/assets/book3.jpg"
    book4.src = "/llb/assets/book4.jpg"
}
function swapImage(thisImage,newImage) {
if (document.images) {
    document[thisImage].src = eval(newImage + ".src")
}


}
function show_visibility(IDS){
hide_visibility();
document.getElementById(IDS).style.display = 'block';
}

function hide_visibility(){
var sel = document.getElementById('bookleadin').getElementsByTagName('div');
for (var i=0; i<sel.length; i++) { sel[i].style.display = 'none'; }

}

</script>
</head>

<body>

<div id="content">
   <div id="books">
      <div id="bookimages">
        <img id="bookcover" name="bookcover" src="../llb/assets/book1.jpg" />
        <ul>
          <li><a href="#" onclick="swapImage('bookcover','book1'); show_visibility('bt1'); this.onmouseout=''" onMouseOver="swapImage('bookcover','book1')" onMouseOut="swapImage('bookcover','book1')">1</a></li>
          <li><a href="#" onclick="swapImage('bookcover','book2'); show_visibility('bt2'); this.onmouseout=''" onMouseOver="swapImage('bookcover','book2')" onMouseOut="swapImage('bookcover','book1')">2</a></li>
          <li><a href="#" onclick="show_visibility('bt3'); swapImage('bookcover','book3')" onMouseOver="swapImage('bookcover','book3')" onMouseOut="swapImage('bookcover','book1')" onclick="swapImage('bookcover','book3')">3</a></li>
          <li><a href="#" onclick="show_visibility('bt4'); swapImage('bookcover','book4')" onMouseOver="swapImage('bookcover','book4')" onMouseOut="swapImage('bookcover','book1')" onclick="swapImage('bookcover','book4')">4</a></li>
        </ul>
      </div><!-- end bookimages -->
      <div id="bookleadin">
        <div id="bt1" style="display:block;"><p>Writing starts with living...</p>
      </div>
        <div id="bt2" style="display:none;"><p>The air is silk...</p>
      </div>
        <div id="bt3" style="display:none;"><p>I lived in the woods...</p>
      </div>
        <div id="bt4" style="display:none;"><p>I tried to forget...</p>
      </div>
    </div><!-- end bookleadin -->
    <div class="spacer"></div>
   </div><!-- ends books -->
</div><!-- end content -->

列表项目#2是我正在测试的示例。我仍然是一个javascript初学者/中级,因此任何答案都必须足够简单,以便我理解并应用于此项目或完成,以便我可以复制/粘贴它。

提前感谢您提供任何帮助。

1 个答案:

答案 0 :(得分:0)

您可以在onclick设置一个标记,如果已点击则会被点击,如果是,则交换onmouseout否则不执行任何操作,但不执行this.onmouseout = ""

编辑:

  <head>
<script>
    if(document.images) {
        book1 = new Image
        book2 = new Image
        book3 = new Image
        book4 = new Image

        book1.src = "/llb/assets/book1.jpg"
        book2.src = "/llb/assets/book2.jpg"
        book3.src = "/llb/assets/book3.jpg"
        book4.src = "/llb/assets/book4.jpg"
    }
    var swap_list = {}; // a list of swapped elements
    function swapImage(thisImage, newImage) {
        if(typeof swap_list[thisImage] == 'undefined' || swap_list[thisImage]=='')
            if(document.images) {
                document[thisImage].src = eval(newImage + ".src");
                swap_list[thisImage] = newImage;
            }
        else
            {
                swap_list[thisImage] = '';
                document[thisImage].src = "../llb/assets/book1.jpg";
            }
    }

    function show_visibility(IDS) {
        hide_visibility();
        document.getElementById(IDS).style.display = 'block';
    }

    function hide_visibility() {
        var sel = document.getElementById('bookleadin').getElementsByTagName('div');
        for(var i = 0; i < sel.length; i++) {
            sel[i].style.display = 'none';
        }

    }
</script>
</head>
<body>
    <div id="content">
        <div id="books">
            <div id="bookimages">
                <img id="bookcover" name="bookcover" src="../llb/assets/book1.jpg" />
                <ul>
                <li>
                        <a href="#" onclick="swapImage('bookcover','book1'); show_visibility('bt1'); " onMouseOver="swapImage('bookcover','book1')" onMouseOut="swapImage('bookcover','book1')">1</a>
                </li>
                <li>
                    <a href="#" onclick="swapImage('bookcover','book2'); show_visibility('bt2'); " onMouseOver="swapImage('bookcover','book2')" onMouseOut="swapImage('bookcover','book1')">2</a>
                </li>
                <li>
                    <a href="#" onclick="show_visibility('bt3'); swapImage('bookcover','book3')" onMouseOver="swapImage('bookcover','book3')" onMouseOut="swapImage('bookcover','book1')" onclick="swapImage('bookcover','book3')">3</a>
                </li>
                <li>
                    <a href="#" onclick="show_visibility('bt4'); swapImage('bookcover','book4')" onMouseOver="swapImage('bookcover','book4')" onMouseOut="swapImage('bookcover','book1')" onclick="swapImage('bookcover','book4')">4</a>
                </li>
            </ul>
        </div><!-- end bookimages -->
        <div id="bookleadin">
            <div id="bt1" style="display:block;">
                <p>
                    Writing starts with living...
                </p>
            </div>
            <div id="bt2" style="display:none;">
                <p>
                    The air is silk...
                </p>
            </div>
            <div id="bt3" style="display:none;">
                <p>
                    I lived in the woods...
                </p>
                </div>
                <div id="bt4" style="display:none;">
                    <p>
                    I tried to forget...
                    </p>
            </div>
            </div><!-- end bookleadin -->
            <div class="spacer"></div>
        </div><!-- ends books -->
    </div><!-- end content -->
</body>