如何将内联javascript移动到脚本标记?

如何将内联javascript移动到脚本标记?,javascript,jquery,Javascript,Jquery,我有一个很好用的popover,下面是popover的html: <div class="popover2" onmouseover="$(this).mouseleave(function() {$(this).hide(); });"> <div class="arrow"></div> <div class="popover2-inner"> <span class="popover2-title"&g

我有一个很好用的popover,下面是popover的html:

<div class="popover2"  onmouseover="$(this).mouseleave(function() {$(this).hide(); });">
    <div class="arrow"></div>
    <div class="popover2-inner">
        <span class="popover2-title">Popover() 2</span>
    <div class="popover2-content">
        <ul class="unstyled">
            <li><span>$200 Bronze</span><p>2 hrs. drivers training</p </li>
        </ul>
    </div>
</div>

但是我想将内联onmouseover javascript内容移动到实例化代码中。我不知道怎么做。任何帮助都很好,谢谢。

您是否尝试过将这行代码放在实例化代码之后

$('.popover2').mouseleave(function() { $(this).hide(); });

只需删除内联并添加:

$('.popover2').mouseleave(function(){
    $(this).hide();
});

在mouseover上只绑定mouseleave事件没有意义,因为mouseover事件必须在mouseleave之前触发(AFAIK)。这样做可以:

$('.popover2').mouseleave(function() {
    $(this).hide();
});
当然,这假设在“实例化代码”运行时存在
.popover2

如果没有,

$('.popover2').live('mouseleave', function() {
    $(this).hide();
});

给标记一个id,然后在JS中绑定事件。除非这是该类的唯一标记,在这种情况下,您可以简单地使用该类。您必须更加具体,我不知道您在说什么,对不起,谢谢!
$('.popover2').live('mouseleave', function() {
    $(this).hide();
});