触发javascript函数onclick
Trigger javascript function onclick
我在mudcu的脚本sketch.js.的帮助下创建了一个简单的绘画应用程序
我在代码中做了一些更改,但现在我遇到了触发一个设置铅笔颜色的函数的问题。
var Sketch = function(config) { "use strict";
var that = this;
...........
var ctx2 = layer2d[2];
// Style object.
this.zoom = 1;
this.style = {
tool: "brush",
globalAlpha: 1,
globalCompositeOperation: "source-over",
strokeStyle: "#000",
lineWidth: 5,
lineCap: "round",
lineJoin: "round"
我的问题是,当用户单击按钮时,我如何将"this.style"strokeStyle设置为例如"#D55151"?
使用JQuery是解决问题的好方法:
<button id="myBtn">My Button</button>
JQuery
$('#myBtn').click(function(){
// set you style
that.style.strokeStyle = "#D55151"; // var that = this;
});
更新:
纯javascript的答案:
document.getElementById('myBtn').onclick=function(){
// set you style
that.style.strokeStyle = "#D55151"; // var that = this;
};
您可以添加:
function(config, style) {
this.style = style || this.style = {
tool: "brush",
globalAlpha: 1,
globalCompositeOperation: "source-over",
strokeStyle: "#000",
lineWidth: 5,
lineCap: "round",
lineJoin: "round"
};
}
并称之为
$("#somebutton").on("click", function() {
Sketch({config}, {overides style});
});
如果您使用的是jQuery或下划线,则更好:
function(config, style) {
this.style = $.extend({
tool: "brush",
globalAlpha: 1,
globalCompositeOperation: "source-over",
strokeStyle: "#000",
lineWidth: 5,
lineCap: "round",
lineJoin: "round"
}, style);
}
我更喜欢第二种更安全的解决方案。。。
相关文章:
- onclick函数需要双击,因为类分配延迟
- Javascript onclick函数会立即调用(而不是在单击时调用)
- jquery Onclick函数带有导致双击的回调排序函数
- 使用element简化onclick函数
- 在React中,为什么我必须绑定onClick函数而不是调用它
- 如何从onclick函数设置全局变量并将其传递给另一个JS文件
- 在onclick函数中传递多个参数
- 如何将onclick函数更改为具有多个函数
- 将OnClick函数设置为<ul>,最后一个ul是擦除第一个ul-s
- 指定一个具有静态值的onclick函数
- Onclick函数更改为所有更改元素的相同函数
- 将php变量传递给onclick函数
- 请帮我完成js中按钮的onclick函数
- .hasClass 在 onclick 函数中不起作用,但删除和添加类函数可以
- Jquery移动转换复选框在Jquery onclick函数()中第二次选中不起作用
- 使用“;这个“;在onclick函数中
- 如何在按Enter键后调用onclick函数
- jQuery onclick 函数:未定义不是一个函数
- 如何在 OnClick 函数中传递字符串参数
- OnClick函数仅在第二次单击时起作用