0
我想创建一个插件'myPlugin',它只是将一些文本添加到div。如:Javascript插件创建方法
document.getElementById('testDiv').myPlugin("this is a text");
我该如何通过单例方法以及原型方法来实现这一点?
我想创建一个插件'myPlugin',它只是将一些文本添加到div。如:Javascript插件创建方法
document.getElementById('testDiv').myPlugin("this is a text");
我该如何通过单例方法以及原型方法来实现这一点?
function appendTextToElement(element, text) {
var textProperty = element.innerText === undefined ? "textContent" : "innerText";
element[textProperty] = element[textProperty] + text;
}
and a test。
HTMLElement.prototype.myPlugin = function(t) {
// ...
}
将为符合标准的浏览器中的所有HTML元素添加一个方法。它不会在IE上工作(至少在旧版本中,我真的没有在IE8中尝试过)。
你是什么意思的“单身法”?
嗨matti,感谢您的重播。 我不想扩展HTMLElement对象。我想要的东西是: function myPlugin(){ this.create = function(text){ div.innerHTML = text; }; } 或 VAR为myplugin = { 创建:功能(文本){ div.innerHTML =文本; } } 我基本上是一个设计师,没有太多的编程知识。 – Aneesh 2010-04-14 09:54:25
而且......你刚刚发布的代码并没有做你想做的事情? – 2010-04-14 10:01:03