やった:
var element = document.getElementById('myelement');
今私はやりたい:
element.myFunction();
しかし、関数内で要素を取得する方法がわかりません。どうすればいいですか?
function myFunction() {
// Get element here?
}
やった:
var element = document.getElementById('myelement');
今私はやりたい:
element.myFunction();
しかし、関数内で要素を取得する方法がわかりません。どうすればいいですか?
function myFunction() {
// Get element here?
}
関数を要素と連鎖させたい場合は、HTMLElement
、またはNode
、オブジェクトのプロトタイプを拡張する必要があります。
HTMLElement.prototype.myFunction = function (parameters) {
var elementToActOn = this;
elementToActOn.style.color = 'green';
return this; // for further chaining
}
または:
Node.prototype.myFunction = function (parameters) {
var elementToActOn = this;
elementToActOn.style.color = 'green';
return this; // for further chaining
}
このアプローチthis
では、メソッド内で、myFunction()
次によって選択された要素が参照されます。
document.getElementById('elementID').myFunction();
HTMLElement
プロトタイプを使用したJS Fiddle demo 。
Node
プロトタイプを使用したJS Fiddle demo 。
同様に、要素ノードを引数として「明示的に」渡すことなく関数に渡すだけの場合は、次のようになります。
function myFunction () {
this.style.color = 'red';
}
document.getElementById('elementID').addEventListener('click', myFunction);
getElementsByTagName()
、getElementsByClassName()
またはquerySelectorAll()
(とりわけ)によって選択された複数の要素を操作したい場合は、代わりに、次のようにNodeList
プロトタイプを拡張する必要があります。
NodeList.prototype.myFunction = function (parameters) {
var elements = this;
[].forEach.call(elements, function(a){
a.style.color = 'green';
});
return this; // for chaining
}
/* calling the function, with a simple demonstration of why you might want to
chain, by returning 'this': */
var len = document.getElementsByClassName('elementClass').myFunction().length;
console.log(len);
あなたが宣言しているのでJavaScriptで
var element = document.getElementById('myelement');
要素は既にグローバル スコープにあるため、関数内で簡単に呼び出すことができます。
function myFunction() {
// Do something with your element
}
編集:代わりに別の関数内で要素を宣言する場合、または異なる要素に同じ関数を使用する場合は、パラメーターを使用する必要があります。これは呼び出しです:
myFunction(element)
そして、これが関数です
function myFunction(element) {
// Do something with your element
}
次のように、カスタム関数をHTMLElement
オブジェクトのプロトタイプに追加できます。
HTMLElement.prototype.customFunc = function(){
// within this function, 'this' will refer
// to the element that it was called on
};
これは、すべての要素がcustomFunc
メソッドとして関数にアクセスできることを意味します。例えば:
HTMLElement.prototype.customFunc = function(){
console.log(this.id);
};
// now assuming your HTML contains #myElement and #anotherElem:
document.getElementById('myElement').customFunc(); // displays 'myElement'
document.getElementById('anotherElem').customFunc(); // displays 'anotherElem'
明らかに、既存のメソッドやプロパティを上書きしたくないので、関数の命名には注意してください。
それはあなたが望むものですか
<input type = "button" value = "Next" id = "btnNext" />
<script>
HTMLElement.prototype.myFunction = function()
{
alert(this.id);
}
var elem = document.getElementById("btnNext");
elem.myFunction();
</script>
与えられた
var element = document.getElementById('myelement');
this
キーワードを使用した関数が必要なようです
function myFunction() {
console.log(this); // logs `this` (which will be `element`)
}
そして、できるようにプロパティを設定するのではなく、orelement.myFunction();
を使用することをお勧めします。Function.prototype.call
Function.prototype.apply
myFunction.call(element); // element gets logged
むしろ、これを試す必要があります。onclick を使用した div の例を示します。
<script>
function myfunc(){
getElementById('sample').whatever function u want
}
</script>
そして体の中であなたが使うことができる -
<body>
<div id="sample" onclick="myfunc()">The function will happen onclicking the div</div>
</body>