簡體   English   中英

在函數中獲取元素

[英]Get element within function

我做了:

var element = document.getElementById('myelement');

現在我想做:

 element.myFunction();

但是我不知道如何在函數中獲取元素。 我該怎么做呢?

function myFunction() {
// Get element here?
}

在JavaScript中,因為您要聲明

var element = document.getElementById('myelement');

您已經在全局范圍內擁有該元素,因此您可以在函數內部簡單地調用它

function myFunction() {
  // Do something with your element
}

編輯:如果要在另一個函數中聲明元素,或者要對不同元素使用相同的函數,則必須使用參數。 這是電話:

myFunction(element)

這就是功能

function myFunction(element) {
  // Do something with your element
}

如果要將函數與元素鏈接,則需要擴展HTMLElementNode對象的原型:

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
}

通過這種方法, myFunction()方法內部的this將引用由以下元素選擇的元素:

document.getElementById('elementID').myFunction();

JS Fiddle演示 ,使用HTMLElement原型。

JS Fiddle演示 ,使用Node原型。

同樣的,如果你想要做的就是將元素節點的功能,無需“明確”它作為參數傳遞:

function myFunction () {
    this.style.color = 'red';
}

document.getElementById('elementID').addEventListener('click', myFunction);

JS小提琴演示

如果您希望對由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);

JS小提琴演示

您可以像這樣將自定義函數添加到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`)
}

而且,與其設置屬性,不如設置element.myFunction(); ,我建議使用Function.prototype.callFunction.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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM