$.fn.show=function(){
alert($(this).val());
}; $("#show").click(function(){$.fn.show();});
<input type="button" value="showval" id="show">这几行代码, 我是想点击button之后弹出showval这个字符串的。但是现在的结果是undefined
求助
alert($(this).val());
}; $("#show").click(function(){$.fn.show();});
<input type="button" value="showval" id="show">这几行代码, 我是想点击button之后弹出showval这个字符串的。但是现在的结果是undefined
求助
alert($(node).val());
}; $("#show").click(function({$.fn.show($("this"));});
<script type="text/javascript">
$("#show").click(function(){
alert($(this).val());
});
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<title>无标题文档</title>
<script type="text/javascript">
(function($){
$.fn.showBox=function(){
//alert(this.attr('value'));
return this.attr('value','#f00');
}
})(jQuery);jQuery(function($){
$("#show").click(function(){
$(this).showBox();
});
});
</script>
</head><body>
<input type="button" value="showval" id="show" />
</body>
</html>
<script type="text/javascript">
(function($){
$.fn.showBox=function(){
//alert(this.attr('value'));
return this.attr('value','#f00');
}
$.alertBox=function(ele){
alert($(ele).attr('value'));
}
})(jQuery);jQuery(function($){
$("#show").click(function(){
//$(this).showBox();
$.alertBox(this);
});
});
</script>
修改后如下://$.fn.show是给每个jquery对象添加了一个show方法以供其调用,所以在这个函数的具体执行中
//this指代的已经是jquery对象无需再加“$”
$.fn.show=function(){
alert(this.val());
};//click函数中的this指的是input元素而非jquery对象,所以需要加“$”转化为jquery对象来调用show方法
$("#show").click(
function(){
$(this).show()
}
);