如何使用jquery使用参数值触发和事件

时间:2013-01-18 07:59:10

标签: jquery

我想在用户鼠标悬停时显示参数值。我想用jquery来做。我的函数返回一个未定义的值。

<script type="text/javascript">
$(function showinfo(id, a,b){
    $('div').mouseover(function(){
        var html= '<div>'+ a +'</div> <div>'+b+'</div>';
        $('body').append(html)
    })
})
</script>

HTML:

<div onmouseover="showinfo(this, '50','60')">show info</div>

3 个答案:

答案 0 :(得分:1)

我建议稍微改变一下:

<div id="container">
    <div id="showInfo" >show info</div>
</div>

和jQuery:

$(document).ready(function() {
  $("#showInfo").mouseover(function() {
    showInfo($(this).attr("id"), '50', '60');
  });
});

function showInfo(id, a, b) {
  var html = '<div>'+a+'</div><div>'+b+'</div>';
  $("#container").append(html);
}

您还可以查看jQuery的悬停功能。 jsFiddle这里:http://jsfiddle.net/KvMuD/

答案 1 :(得分:0)

您遇到了一些语法错误:

<script type="text/javascript">
$(document).ready(function(){
    function showinfo(id, a,b){
        var html= '<div>'+ a +'</div> <div>'+b+'</div>';
        $('body').append(html);
    }
});
</script>

答案 2 :(得分:0)

function showInfo(id, a, b) {
  var html = '<div>'+a+'</div><div>'+b+'</div>';
  $(body).append(html);
}

然后:

<div onmouseover="showinfo(this, '50','60')">show info</div>

将是一个更清洁的版本。但是,为什么你要将你正在盘旋的实际div传递给函数并不清楚。你想用它做什么?