如何通过原型函数参数获取 id?
How do get a id through a prototype function parameter?
你好,刚刚完成我的 Javascript 东西,运行 遇到了一个小问题,这没有任何意义,为什么它不起作用?
我想通过我的原型函数主体使用 id "prototype function parameter",但由于某种原因它不起作用?这是语法错误吗?我还缺少什么吗?我在哪里可以简单地了解更多相关信息.
function BG(type) {
this.type = type;
}
BG.prototype.RootFrame = function (x, y, size, title, id) {
// document.write("hello!" + x);
var RF = document.getElementsByTagName(id);
RF.innerHTML = 'Hello!';
};
var BG = new BG();
var execute = function () {
BG.RootFrame(0, 0, 0, 0, 'test');
};
if (!!(window.addEventListener))
window.addEventListener("DOMContentLoaded", execute)
else
window.attachEvent("onload", execute)
此代码有效:
function BG(type) {
this.type = type;
}
BG.prototype.RootFrame = function (x, y, size, title, id) {
// document.write("hellocrap!" + x);
var RF = document.getElementById(id);
RF.innerHTML = 'Hello!';
};
var BG = new BG();
var execute = function () {
BG.RootFrame(0, 0, 0, 0, 'test');
};
if (!!(window.addEventListener))
window.addEventListener("DOMContentLoaded", execute)
else
window.attachEvent("onload", execute)
在我看来,您正在发送一个元素的 id,并且您想对 select 该元素使用 getElementsByTagName。
尝试改用 getElementById。
此外,既然您想使用原型...您可以只使用 $("yourIdHere").
另请注意...您要查找的 ID 必须存在于 运行 脚本之前(因此您应该在页面加载后调用该函数)。
getElementsByTagName 等待 "html tag" 和 'test' 似乎是一个 ID。
尝试改用 getElementsById!
PS:
var BG = new BG();
var bg = new BG(); // better ;-)
如果 <test></test>
在 html 中有一个元素,它工作正常,只需获取第一个节点 [0]
,因为 getElementsBytagName
returns 中的所有元素文档
var RF = document.getElementsByTagName(id)[0];
你好,刚刚完成我的 Javascript 东西,运行 遇到了一个小问题,这没有任何意义,为什么它不起作用?
我想通过我的原型函数主体使用 id "prototype function parameter",但由于某种原因它不起作用?这是语法错误吗?我还缺少什么吗?我在哪里可以简单地了解更多相关信息.
function BG(type) {
this.type = type;
}
BG.prototype.RootFrame = function (x, y, size, title, id) {
// document.write("hello!" + x);
var RF = document.getElementsByTagName(id);
RF.innerHTML = 'Hello!';
};
var BG = new BG();
var execute = function () {
BG.RootFrame(0, 0, 0, 0, 'test');
};
if (!!(window.addEventListener))
window.addEventListener("DOMContentLoaded", execute)
else
window.attachEvent("onload", execute)
此代码有效:
function BG(type) {
this.type = type;
}
BG.prototype.RootFrame = function (x, y, size, title, id) {
// document.write("hellocrap!" + x);
var RF = document.getElementById(id);
RF.innerHTML = 'Hello!';
};
var BG = new BG();
var execute = function () {
BG.RootFrame(0, 0, 0, 0, 'test');
};
if (!!(window.addEventListener))
window.addEventListener("DOMContentLoaded", execute)
else
window.attachEvent("onload", execute)
在我看来,您正在发送一个元素的 id,并且您想对 select 该元素使用 getElementsByTagName。
尝试改用 getElementById。
此外,既然您想使用原型...您可以只使用 $("yourIdHere").
另请注意...您要查找的 ID 必须存在于 运行 脚本之前(因此您应该在页面加载后调用该函数)。
getElementsByTagName 等待 "html tag" 和 'test' 似乎是一个 ID。
尝试改用 getElementsById!
PS:
var BG = new BG();
var bg = new BG(); // better ;-)
如果 <test></test>
在 html 中有一个元素,它工作正常,只需获取第一个节点 [0]
,因为 getElementsBytagName
returns 中的所有元素文档
var RF = document.getElementsByTagName(id)[0];