Dea*_*ool 2 javascript jquery plugins function
我正在制作一个简单的插件函数,在单击时将元素的背景颜色变为红色.我添加了如下所示的代码,但无法在点击时获得按钮的背景颜色.有人可以更正代码吗?
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$.fn.red({
red:function(){return this.each(function(){this.style.background=red;})}
});
</script>
</head>
<body>
<button onclick="red()">Red</button>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
您还没有定义什么red
应该做的.你需要这样的东西:
$.fn.red = function(/*options object maybe?*/) {
return this.css( "background-color", "red" );
};
Run Code Online (Sandbox Code Playgroud)
使用jQuery不需要像这样的内联事件:
$(function() {
$("button").on("click", function() {
$(this).red();
});
});
Run Code Online (Sandbox Code Playgroud)
您应该先阅读本教程:
https://learn.jquery.com/plugins/basic-plugin-creation/
归档时间: |
|
查看次数: |
426 次 |
最近记录: |