Jquery 单击此按钮可将一个变量附加到多个变量
嗨,我有一个这样的工作代码Jquery 单击此按钮可将一个变量附加到多个变量,jquery,Jquery,嗨,我有一个这样的工作代码 $(document).on ("click", ".submit", function () { the_id = $(this).attr('data') ; }); $(document).on ("mouseover", ".edit", function () { the_id = $(this).attr('data') ; }); $(document).on ("mouseout", ".avoi
$(document).on ("click", ".submit", function () {
the_id = $(this).attr('data') ;
});
$(document).on ("mouseover", ".edit", function () {
the_id = $(this).attr('data') ;
});
$(document).on ("mouseout", ".avoid", function () {
the_id = $(this).attr('data') ;
});
但是它看起来很像注释这个变量这个id
,因为每次我点击鼠标时,我都要定义它,我有很多onclick onmouseover都使用相同的变量
问题是我无法将其作为全局变量,因为它有$(this)
,它获取单击元素的id
我怎样才能做到这一点:
the_id = $(this).attr('data') ; // how to define it here with $this() <-------
$(document).on ("click", ".submit", function () {
// its defined here the_id ;
});
$(document).on ("mouseover", ".edit", function () {
//its defined here the_id ;
});
$(document).on ("mouseout", ".avoid", function () {
// its defined here the_id ;
});
通过指定所有以逗号分隔的元素,可以将这3行合并为一行,如下所示:
$(document).on ("click mouseover mouseout", ".submit, .edit, .avoid", function () {
the_id = $(this).attr('data') ;
});
或者您可以保留3行,但调用相同的函数:
function doAction() {
the_id = $(this).attr('data');
}
$(document).on ("click", ".submit", doAction);
$(document).on ("mouseover", ".edit", doAction);
$(document).on ("mouseout", ".avoid", doAction);
只需定义脚本标记open的变量top,脚本中的所有函数都可以使用该变量。如下
<script>
var the_id = '';
__YOUR CODE__
</script>
var_id=“”;
__你的代码__
是的,对不起,我指的是这个编辑,我真的知道这个解决方案,看我的编辑,它不仅仅是clicy,你也可以对事件使用相同的方法,但不使用逗号。看看我的编辑。:)不起作用:(.将提交我回家后做的事情it saids doAction不是一个函数您的代码看起来很好。如果它说函数未定义,那么很可能函数在您尝试调用它的范围内不可见。
<script>
var the_id = '';
__YOUR CODE__
</script>