简体   繁体   English

jQuery mouseover事件不起作用

[英]jquery mouseover event not working

I'm trying to use the Show Hint script from dynamicdrive.com to show form errors on my website. 我正在尝试使用dynamicdrive.com上的Show Hint脚本在我的网站上显示表单错误。

Currently the error is displayed as 当前错误显示为

  <span class="error">Error Message</span>

when there is an error with one of the inputs. 输入之一出现错误时。

I would like to use the show hint script to display this error instead, then i will have to edit every page which has error on it by replacing 我想使用显示提示脚本来显示此错误,然后我将不得不通过替换来编辑每个有错误的页面

  <span class="error">Error Message</span>

with

    <span class="error erroranchor" onmouseover="showhint('Error Message Here', this, event, '200px')"></span>

manually. 手动。

But i'm trying to save myself some energy here, this is why i've decided to use JQuery instead, I'm new to query and this will be my first coding loool, and from a logical view i thought JQuery will be the best. 但是我想在这里节省一些精力,这就是为什么我决定改用JQuery的原因,我是查询的新手,这将是我的第一个编码傻瓜,从逻辑上看,我认为JQuery将是最好。 Here's what i've done so far but nothing is working. 这是我到目前为止所做的,但没有任何效果。

<script type='text/javascript'>
//On document ready.. I'm not sure if it should be document ready or document load.
    $(document).ready(function() {
//First i add the class "erroranchor" to <span class="error">
    $(".error").addClass("erroranchor");
//Then i've tried to convert the onmouseover event to JQuery mouseover event.
    $(".erroranchor").mouseover(showhint(){
//Here i try to get the "Error Message" from <span class="error">Error Message</span> so it can be displayed in the hint box.
        var $errortext = $(this);
        $errortext.text(),
        this.event,
        '200px'
    });
    });
</script>

Then there is the CSS which works perfectly as i want.. 然后是CSS,它可以按照我的要求完美地工作。

.error {
    text-indent: -99999px;
}
.erroranchor {
    background: url(anchor.png);
}

I'd appreciate if any improvement can be made on the JavaScript above (please keep it easy to understand). 如果可以在上述JavaScript上进行任何改进(请使其易于理解),我将不胜感激。

Your mouseover binding is wrong. 您的mouseover绑定是错误的。 It should be 它应该是

$(".erroranchor").mouseover(function(){...});

Notice it is function instead of showhint . 注意,它是function而不是showhint

If you want to create the showhint() function as its own function elsewhere in your code, then the binding would be 如果要在代码的其他位置创建showhint()函数作为其自己的函数,则绑定将是

$(".erroranchor").mouseover(showhint);

Edit 编辑
If you want to have a different error message for each '.erroranchor' , you could user jQuery's data() functionality: 如果您希望每个'.erroranchor'都有不同的错误消息,则可以使用jQuery的data()功能:

<span class="error erroranchor" data-errormessage="Error message here"></span>
<span class="error erroranchor" data-errormessage="Different error message here"></span>

Then your eventhandler and function: 然后,您的事件处理程序和函数:

$(".erroranchor").mouseover(showhint);

function showhint() {
    var errMsg = $(this).data('errormessage');
    if(errMsg) {
        //do stuff
    }
}

try this 尝试这个

$(".erroranchor").mouseover(function(){
         showhint();
});

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

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