javascript 当鼠标不再位于导航链接上时,如何调用函数?

svmlkihl  于 2023-01-24  发布在  Java
关注(0)|答案(3)|浏览(107)

我的HTML文件中有这样的导航代码:

<div id="center_links">
            <ul>
                <li><a href="#" onMouseOver="javascript:setSideText(1)">about</a></li>
                <li><a href="blog" onMouseOver="javascript:setSideText(2)">blog</a></li>

...等等。
我的JavaScript看起来像这样:

function setSideText(setting)
{
    if (setting == 0) // Home
    {
        document.getElementById('center_text').innerHTML = '<p><div class="dropcap">#</div>I am an information technology student interested in free and open source software.</p>';
    }
    else if (setting == 1) // About
    {
        document.getElementById('center_text').innerHTML = '<p><div class="dropcap">#</div>My name is David Gay, and this is my website. Welcome.</p>';
    }
    else if (setting == 2) // Blog
    {
        document.getElementById('center_text').innerHTML = '<p><div class="dropcap">#</div>My blog runs on the <a href="http://chyrp.net/">Chyrp</a> blog software.';
    }

当我mouseover一个链接时,页面上的侧边文本会更改以描述该链接。我希望当我不将鼠标放在导航链接上时,文本会更改回默认值(setSideText(0))。我已经玩了一段时间了,但我还没有弄清楚。
我试着将其添加到JavaScript文件中,但没有效果:

document.getElementById('center_links').onMouseOut = setSideText(0);

我想反正也没用。
我肯定有一个简单的解决方案,我没有想到(我只是拿起语言)。任何指导是赞赏!

xuo3flqw

xuo3flqw1#

我有两个主要建议,第一:不要使用内联事件处理程序(将行为放在一个地方更易于维护),第二个是在父center_links元素上使用onmouseout
为此目的:

function setSideText(setting) {
    if (setting == 0) // Home
    {
        document.getElementById('center_text').innerHTML = '<p><div class="dropcap">#</div>I am an information technology student interested in free and open source software.</p>';
    }
    else if (setting == 1) // About
    {
        document.getElementById('center_text').innerHTML = '<p><div class="dropcap">#</div>My name is David Gay, and this is my website. Welcome.</p>';
    }
    else if (setting == 2) // Blog
    {
        document.getElementById('center_text').innerHTML = '<p><div class="dropcap">#</div>My blog runs on the <a href="http://chyrp.net/">Chyrp</a> blog software.';
    }
}

var linksElem = document.getElementById('center_links'),
    links = linksElem.getElementsByTagName('a');

for (var i = 0, len = links.length; i < len; i++) {
    links[i].dataIndex = i+1;
    links[i].onmouseover = function() {
        setSideText(this.dataIndex);
    };
}

linksElem.onmouseout = function(){
    setSideText(0);
}

JS Fiddle demo.

    • 编辑**修改setSideText()函数,使其响应单词而不是索引(因为我认为在以后添加后续链接更容易,并且不依赖于能够向元素添加任意属性,尽管它 * 确实 * 要求元素具有id属性...):
function setSideText(setting) {
    if (setting == 'home' || setting == 0)
    {
        document.getElementById('center_text').innerHTML = '<p><div class="dropcap">#</div>I am an information technology student interested in free and open source software.</p>';
    }
    else if (setting == 'about')
    {
        document.getElementById('center_text').innerHTML = '<p><div class="dropcap">#</div>My name is David Gay, and this is my website. Welcome.</p>';
    }
    else if (setting == 'blog')
    {
        document.getElementById('center_text').innerHTML = '<p><div class="dropcap">#</div>My blog runs on the <a href="http://chyrp.net/">Chyrp</a> blog software.';
    }
}

var linksElem = document.getElementById('center_links'),
    links = linksElem.getElementsByTagName('a');

for (var i = 0, len = links.length; i < len; i++) {
    links[i].onmouseover = function() {
        setSideText(this.id);
    };
}

linksElem.onmouseout = function(){
    setSideText(0);
}

JS Fiddle demo.

mjqavswn

mjqavswn2#

这不起作用的唯一原因是在Javascript中设置这些DOM事件时,没有大写;只需将.onMouseOut更改为.onmouseout即可。
我不知道他们为什么决定让这些事件的HTML和Javascript名称不一致(我想这是人们讨厌DOM的另一个原因)。

w8f9ii69

w8f9ii693#

触发onMouseOut函数的方式与触发onMouseOver函数的方式相同
您的链接HTML需要看起来像这样:

<a href="blog" onMouseOver="javascript:setSideText(2)" onMouseOut="javascript:setSideText(0)">blog</a>

我建议研究一下jQuery--它使处理事件和DOM操作更加直接!
这里有很棒的免费课程:http://tutsplus.com/course/30-days-to-learn-jquery/

相关问题