<! Doctype html> <html> <head> <meta charset = "utf-8"> <estilo> botão {background-color:#0f0;} </style> </head> <body> <butt ID = "Buttonement"> Button </Button> <input Type = "text"> <cript> var = document.gão "> {alert (this.id); // botão pop-up}; // no contexto pode ser visto como botão </sCript> </body> </html>Junte -se a Bind
A cópia do código é a seguinte:
var text = document.getElementById ("text");
var Button = document.getElementById ("Button");
Button.OnClick = function () {
alerta (this.id); // botão pop-up
} .bind (texto);
// isso no contexto pode ser visto como botão
Neste momento, você descobrirá que isso mudou para o texto
Também é aplicável em literais de função, com o objetivo de manter a direção para cima e para baixo (isso) inalterado.
var obj = {color: "#ccc", elemento: document.getElementById ('text'), events: function () {document.getElementById ("botão"). addEventListener ("clique", funclor (e) {console.log (this); this.Element.style.color = this.color; {this.events ();}}; obj.init ();No momento, clicar no texto do botão mudará de cor. Pode -se observar que este não é um botão, mas obj.
O método de Bind () não é aplicável no IE, 6, 7, 8. É necessário estender esse método, estendendo o protótipo da função.
if (! function.prototype.bind) {function.prototype.bind = function (obj) {var slice = [] .slice, args = slice.call (argumentos, 1), self = this, nop = function () {}, limite = function () {return self.Apply (this instância de this: {}), args.concat (slice.call (argumentos)));}; nop.prototype = self.prototype; bound.prototype = new nop (); retorno limitado;};}Neste momento, você pode ver que o bind () também é suportado em IE6, 7 e 8.
A cópia do código é a seguinte:
slice = array.prototype.slice,
ou
Array = array.prototype.slice.call (Array, 0);
Converter uma matriz como uma matriz