开发者

Create instance of HTMLInputElement

I want to extract a user name and password pair and i开发者_JAVA百科ts corresponding form element on a 'Log in' page by analyzing Document Object Model (DOM) objects.

First, all the HTMLInputElement objects within the HTMLDocument object of the 'Log in' page are to be collected. Next, the password object is located by examining its special attribute type="password".


Answering the body of your question:

You can find elements using document.getElementsByTagName. E.g.:

function findThePasswords() {
    var inputs = document.getElementsByTagName('input'),
        input,
        index;
    for (index = 0; index < inputs.length; ++index) {
        input = inputs[index];
        if (input.type === 'password') {
            // Whatever
        }
    }
}

Use this knowledge only to benefit Humanity, not in an evil quest for global domination. ;-)

Answering the title of your question:

You create elements via document.createElement, e.g.:

var input = document.createElement('input');
input.type = "password";
document.getElementById('someForm').appendChild(input);


document.getElementById
document.getElementsByTagName

For Examply:

document.getElementById('navigation').getElementsByTagName('a')[3];
    returns the fourth link inside the element with the ID 'navigation'
document.getElementsByTagName('div')[2].getElementsByTagName('p')[0];
    returns the first paragraph inside the third div in the document.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜