原型功能不会在onclick& amp;无论如何都没有正确执行

时间:2017-03-18 00:58:37

标签: javascript html css

此次尝试存在两个问题:

1)我不知道如何让按钮调用JS原型函数onclick,因为原型函数没有被命名为普通函数

2)原型函数显然应该运行并使用JS函数中描述的文本更改段落标记,但它不会运行

有人可以告诉我这里做错了什么吗?特别是数字2,因为我在编码中看不到任何错误。

非常感谢帮助,谢谢。



function house(bedrooms, bathrooms, floors) {
  this.bedrooms = bedrooms;
  this.bathrooms = bathrooms;
  this.floors = floors;
  // objects
}

house.prototype.methodexample = function() {
  document.getElementById("prototype-example").innerHTML = "The prototype property can also be used with functions.";
}

@charset "UTF-8";

/* CSS Document */

body {
  height: 1000px;
  width: 100%;
  background: #fff;
  margin: 0;
}

#javascript-essentials {
  width: 100%;
  height: auto;
  margin: 10px;
  padding: 10px;
  background: #eff2f7;
}

#javascript-programming-techniques {
  width: 100%;
  height: auto;
  margin: 10px;
  padding: 10px;
  background: #F0F9FC;
}

.divider {
  width: 100%;
  height: auto;
  background: #CCC;
  display: block;
  margin: 10px;
}

h1 {
  font-size: 25px;
  display: block;
  width: 100%;
  height: auto;
  margin: 10px;
  text-decoration: underline;
}

h2 {
  font-size: 20px;
  display: block;
  width: 100%;
  height: auto;
  margin: 10px;
  text-decoration: underline;
}

h3 {
  font-size: 16px;
  display: block;
}

#prototype-example {}

<!-- Checklist: Prototype -->
<article class="divider">
  <h3>Prototype Property</h3>
  <p>The prototype property adds new properties or methods to Javascript objects.</p>
  <p>NOT WORKING!!!!!!!!!!!!!</p>
  <p id="prototype-example"></p>
  <button onclick="______________">Click Me</button>
</article>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:2)

您需要创建一个private void PopulateData() { while (condition) //Columns[0] is where your ComboBoxColumn index (dataGridView1.Columns[0] as DataGridViewComboBoxColumn) .Items.Add("Item 1"); } 对象,然后在该对象上调用该方法。

&#13;
&#13;
house
&#13;
function house(bedrooms, bathrooms, floors) {
  this.bedrooms = bedrooms;
  this.bathrooms = bathrooms;
  this.floors = floors;
  // objects
}

house.prototype.methodexample = function() {
  document.getElementById("prototype-example").innerHTML = "The house has " + this.floors + " floors";
}

var myHouse = new house(2, 2, 3);
&#13;
@charset "UTF-8";

/* CSS Document */

body {
  height: 1000px;
  width: 100%;
  background: #fff;
  margin: 0;
}

#javascript-essentials {
  width: 100%;
  height: auto;
  margin: 10px;
  padding: 10px;
  background: #eff2f7;
}

#javascript-programming-techniques {
  width: 100%;
  height: auto;
  margin: 10px;
  padding: 10px;
  background: #F0F9FC;
}

.divider {
  width: 100%;
  height: auto;
  background: #CCC;
  display: block;
  margin: 10px;
}

h1 {
  font-size: 25px;
  display: block;
  width: 100%;
  height: auto;
  margin: 10px;
  text-decoration: underline;
}

h2 {
  font-size: 20px;
  display: block;
  width: 100%;
  height: auto;
  margin: 10px;
  text-decoration: underline;
}

h3 {
  font-size: 16px;
  display: block;
}

#prototype-example {}
&#13;
&#13;
&#13;

答案 1 :(得分:1)

你必须用关键字new实例化house,并且方法的任何失败查找都会自动委托给原型

   function house(bedrooms, bathrooms, floors) {
      this.bedrooms = bedrooms;
      this.bathrooms = bathrooms;
      this.floors = floors;
      // objects
    }

    house.prototype.methodexample = function() {
      document.getElementById("prototype-example").innerHTML = "The prototype property can also be used with functions.";
    }

    document.getElementById('button').addEventListener('click' , (new house).methodexample)

这是你如何从html打电话

  <button id='button' onClick="(new house).methodexample">Click Me</button>

答案 2 :(得分:1)

就像aeid所说,您可以将处理函数传递给按钮,或者甚至更好地将eventListener附加到它。

我建议您避免使用构造函数和新的&#39;。使用对象文字或工厂函数可以使事情变得更清晰,更容易,例如:

function house (bedrooms, bathrooms, floors) {
  return {
    bedrooms: bedrooms,
    bathrooms: bathrooms,
    floors: floors,
    methodExample: function () {
      document.getElementById("prototype-example").innerHTML = "The house has " + floors + " floors";
    }
  }
}

var MyHouse = house(3, 3, 2)

document.getElementById('button').addEventListener('click' , MyHouse.methodexample)

你得到了一个闭包的优势,它比构造函数好得多。

或者,您可以先使用工厂创建对象,然后使用.prototype

附加方法

ps:使用es6,它看起来更好