简体   繁体   English

我如何让一个 onclick 函数更改另一个 onclick 函数的属性?

[英]How do I have an onclick function change the properties of another onclick function?

To help visualize what I'm after.帮助形象化我所追求的。 I have a button with an onclick() that increments the value of an input by 1我有一个带有onclick()的按钮,它将输入的值增加 1

\\ HTML looks like this
<button class="clickme" onclick="pluspotato()">Potato</button>

<script>
var potatocount = 0;
function pluspotato() {
      potatocount = potatocount + 1;
      document.getElementById("potatonum").value = potatocount;
      document.title = potatocount + " Potatoes";
    }
</script>

Now I want to add a button that will change the property of the pluspotato() function to multiply by 2 .现在我想添加一个按钮,将pluspotato()函数的属性更改为乘以2

Any help would be greatly appreciated.任何帮助将不胜感激。

If youd like to solve this properly (so that it scales for further enhancements / development) id suggest you read up on Observables.如果您想正确解决这个问题(以便它可以扩展以进行进一步的增强/开发),id 建议您阅读 Observables。 Im am going to write a simple implementation of one and explain it here.我将编写一个简单的实现并在此处进行解释。

Since you want to change a value at multiple points in your code and read it potentially at multiple points youd have to come up with some sort of interface that allows participants (eg gui elements) to listen to changes made to it (and uptdate the gui accordingly).由于您想在代码中的多个点更改值并可能在多个点读取它,因此您必须想出某种界面,允许参与者(例如 gui 元素)听取对其所做的更改(并更新 gui因此)。

Since this is a quite often needed functionality it is best to write an generally applieable solution for this.由于这是一个经常需要的功能,因此最好为此编写一个普遍适用的解决方案。 Like this Class:像这个类:

class Observable {
  constructor(initalValue) {
    // here come the subscriptions / perticepants that want to listen to changes
    this.listeners = []
    // this is the actual wrapped value. Which can basically be anything. The only 
    // important thing is that nothing changes this property outside of this class.
    // A convention is to mark those properties / functions with an underscore.
    this._value = initalValue
  }
  setValue(value) {
    // first check if the current value is not already the same as the new one.
    // if so: just do nothing
    if (this._value === value) return
    // then set the stored value (so that it can be getted)
    this._value = value
    // loop through all listeners and call them with the now value
    for (let i = 0; i < this.listeners.length; i++) {
      this.listeners[i](value)
    }
  }
  getValue() {
    return this._value
  }
  subscribe(func) {
    // add new listeners to array so that it gets called on setValue
    this.listeners.push(func)

    // Optional: 
    // call added function immediately with current value
    func(this._value)
  }
  unsubscribe(func) {
    //should also exist
  }
}

This class now allows you to add such behaviour.此类现在允许您添加此类行为。

  let observableCounter = new Observable(0)


  function incrementClick() {
    observableCounter.setValue(observableCounter.getValue() + 1)
  }
  function doubleClick() {
    observableCounter.setValue(observableCounter.getValue() * 2)
  }


  // then simply listen to changes everywhere you want the gui to update
  function update1(value) {
    console.log("Updateing GUI to " + value)
    // document.getElementById()...

    // Side note: dont document.getElementById here. If the element doesnt change,
    // getElement once outside update1 and then simply take the reference here.
    // This way on every change the element needs to be found from scartch.
  }

  observableCounter.subscribe(update1)

You can change the element's onclick function to a function that multiplies.您可以将元素的onclick函数更改为乘法函数。

function multpotato() {
    potatocount *= 2;
    document.getElementById("potatonum").value = potatocount;
    document.title = potatocount + " Potatoes";
}

document.getElementById("change").addEventListener("click", function() {
    document.querySelector(".clickme").onclick = multpotato;
});

You may do conditional operation in pluspotato() depending on activation of the second button:您可以根据第二个按钮的激活情况在 pluspotato() 中进行条件操作:

 var potatocount = 0; var operation = 'add1'; function pluspotato() { let potatocount; if(operation === 'multiply2') { potatocount = Number(document.getElementById("potatonum").value) * 2; } else{ potatocount = Number(document.getElementById("potatonum").value) + 1; } document.getElementById("potatonum").value = potatocount; document.title = potatocount + " Potatoes"; } function changePluspotato() { operation = 'multiply2'; }
 <button class="clickme" onclick="pluspotato()">Potato</button> <input id="potatonum"></input><br> <button id="change" onclick="changePluspotato()">changePluspotato</button>

Once you click the second button, the potato button starts to multiply by 2单击第二个按钮后,马铃薯按钮开始乘以 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM