1

我在这里和互联网上搜索了可以重新启用代码中onMouseOut已禁用的onClick代码。我找到了一些东西,但没有什么适合我的情况。

在页面上有一个容器 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 初学者/中级人员,因此任何答案都必须足够简单,以便我理解并应用于该项目或完成,以便我可以复制/粘贴它。

4

1 回答 1

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>
于 2012-01-06T00:31:34.193 回答