简体   繁体   English

在阻止默认行为的同时使用事件处理程序调用函数

[英]Using an event handler to invoke a function while preventing default behaviour

I have been trying to create a dynamic JavaScript image gallery that can be navigated from one page, without the need for the user to navigate to another page. 我一直在尝试创建一个动态JavaScript图像库,该图像库可从一个页面导航,而无需用户导航到另一页面。

function showPic(whichPic) { 
    var source = whichpic.getAttribute("href");  
    var placeholder = document.getElementById("placeholder"); 
    placeholder.setAttribute("src",source); 
}

I am trying to invoke the showPic function with the onlick event handler, while also preventing the default behavior of the element that takes the user to the destination of the link. 我正在尝试使用onlick事件处理程序调用showPic函数,同时还阻止了将用户带到链接目标的元素的默认行为。 I thought that by adding a return false statement to to the JavaScript contained by the onclick event handler I could get this functionality. 我认为,通过向onclick事件处理程序所包含的JavaScript中添加一个return false语句,我可以获得此功能。 It seems not to be working and I cannot understand why. 似乎没有用,我不明白为什么。

Here is my HTML markup: 这是我的HTML标记:

<h1>Breaking Bad Characters</h1>
<ul>
    <li>
        <a href="images/tuco.jpg" onclick="showPic(this); return false;" title="Tuco">Tuco Salamanca</a>
    </li>
    <li>
        <a href="images/walt.jpg" onclick="showPic(this); return false;" title="Walt">Walter White</a>
    </li>
    <li>
        <a href="images/gus.jpg" onclick="showPic(this); return false;" title="Gustavo">Gustavo Fring</a>
    </li>
    <li>
        <a href="images/saul.jpg" onclick="showPic(this); return false;" title="Saul">Saul Goodman</a>
    </li>
    <img id="placeholder" src="images/placeholder.gif" alt="my image gallery" />
</ul>

I can also provide a link to the work: 我还可以提供工作链接:

http://the-session.co.uk/JSgallery/ http://the-session.co.uk/JSgallery/

You'll need to prevent the default from happening by calling event.preventDefault() . 您需要通过调用event.preventDefault()来防止发生默认情况。

function showPic(el) { 
    event.preventDefault();

    var source = el.getAttribute("href");  
    var placeholder = document.getElementById("placeholder"); 
    placeholder.setAttribute("src",source); 
}

<ul>
    <li>
        <a href="images/tuco.jpg" onclick="showPic(this)" title="Tuco">Tuco Salamanca</a>
    </li>
    <li>
        <a href="images/walt.jpg" onclick="showPic(this)" title="Walt">Walter White</a>
    </li>
    <li>
        <a href="images/gus.jpg" onclick="showPic(this)" title="Gustavo">Gustavo Fring</a>
    </li>
    <li>
        <a href="images/saul.jpg" onclick="showPic(this)" title="Saul">Saul Goodman</a>
    </li>
    <img id="placeholder" src="images/placeholder.gif" alt="my image gallery" />
</ul>

See here on MDN - https://developer.mozilla.org/en/docs/DOM/event.preventDefault 请在MDN上查看-https: //developer.mozilla.org/en/docs/DOM/event.preventDefault

You can also play with this on JSFiddle - http://jsfiddle.net/GuDeA/ 您也可以在JSFiddle上玩这个游戏-http: //jsfiddle.net/GuDeA/

Your solution (using return false ) should be fine for IE, and Lloyd's solution (using event.preventDefault() ) should work for other browsers. 您的解决方案(使用return false )对于IE应该很好,而劳埃德的解决方案(使用event.preventDefault() )对于其他浏览器也可以使用。

To work in all browsers, you could use a library like jQuery, which handles the cross-browser issues in it's preventDefault() method . 要在所有浏览器中工作,您可以使用jQuery之类的库,该库通过preventDefault() 方法处理跨浏览器的问题。

If you don't want to use a library, then you could test whether the event has a preventDefault method before calling it: 如果您不想使用库,则可以在调用事件之前测试该事件是否具有preventDefault方法:

if(event.preventDefault) {
    event.preventDefault(); // handles most browsers
}
else {
    return false;           // handles ie
}

the easiest way in function showPic 函数showPic的最简单方法

function showPic(whichPic) { 
    var source = whichpic.getAttribute("href");  
    var placeholder = document.getElementById("placeholder"); 
    placeholder.setAttribute("src",source); 

    var event = window.event;
    if(event.preventDefault) {
        event.preventDefault();
    }
    else {
        return false;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM