触发javascript函数onclick

时间:2013-07-18 09:48:12

标签: javascript

我在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”?

2 个答案:

答案 0 :(得分:0)

使用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;
};

答案 1 :(得分:-1)

你可以添加:

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);
}

我更喜欢第二种解决方案更安全......