Javascript 条件函数的结构?
我有多个html dom文档,有些元素在不同的文档上具有相同的id,我只想编写一个函数来查询传递给它的任何文档,我将解释: 目前我有以下结构:Javascript 条件函数的结构?,javascript,Javascript,我有多个html dom文档,有些元素在不同的文档上具有相同的id,我只想编写一个函数来查询传递给它的任何文档,我将解释: 目前我有以下结构: If(document.getElementById('myid1') != null) // do Something If(document.getElementById('myid2') != null) // do Something If(document.getElementById('myid3') != null) // do Som
If(document.getElementById('myid1') != null)
// do Something
If(document.getElementById('myid2') != null)
// do Something
If(document.getElementById('myid3') != null)
// do Something
If(document.getElementById('myid4') != null)
// do Something
If(document.getElementById('myid5') != null)
// do Something
If(document.getElementById('myid6') != null)
// do Something
有没有更好的方法来组织这个?
如果我为每个页面(文档)编写一个函数,我将有30个函数做同样的事情,所以我考虑只编写一个全局函数。有什么建议吗
编辑:
If(document.getElementById('name') != null)
document.getElementById('name').value = name;
If(document.getElementById('company') != null)
document.getElementById('company').value = com;
If(document.getElementById('email') != null)
document.getElementById('email').value = email;
If(document.getElementById('mail') != null)
document.getElementById('mail').value = email;
提前感谢。您可以在所有页面中创建一个JS文件并导入一个JS文件。在该JS页面中,然后以一种可以识别来自哪个页面的请求的方式进行写入,如果已知,您将了解该页面中的元素。您可以在所有页面中创建一个JS文件并导入一个JS文件。在该JS页面中,然后以一种可以识别来自哪个页面的请求的方式进行写入,如果已知该请求,您将了解该页面中的元素。
for(var i=0;ifor(var i = 0; i < max_number; ++i)
{
If(document.getElementById('myid'+i) != null)
// do Something
}
{
If(document.getElementById('myid'+i)!=null)
//做点什么
}
用于(变量i=0;i
在注释中,您注意到所有“做点什么”位所做的都是填充值
属性。我认为这是一种常见的结构
在最简单的形式中,您需要保留一个元素ID和值对的列表,以尝试将其放入这些元素中。由于元素ID是唯一的,我认为映射是合适的:
var values = {
myid1: "Hello, world! You're looking at the element with the ID of myid1.",
myid2: "I'm the element with the ID of myid2!"
};
显然,接下来的过程是通过成对循环,寻找元素。如果存在,则设置该值;否则,这不是问题:继续前进
在JavaScript中,您可以使用for
。In
循环来循环对象中的对。不过,这有点棘手,因为您只想检查自己的属性;也就是说,特定地属于该对象,并且不是从其他地方继承的属性
总之,当把它们放在一起时,你可能会得到这样的结果:
for(var id in values) {
if(Object.prototype.hasOwnProperty.call(values, id)) {
var element = document.getElementById(id);
if(element !== null) {
element.value = values[id];
}
}
}
var values = {
// If there's something on the page with an ID of nameDisplay, prompt the
// user for what to fill it with.
nameDisplay: function() {
return prompt("What's your name?", "");
},
// If there's an element with an ID of time on the page, fill it with the
// current time.
time: function() {
var now = new Date();
return now.toString();
}
};
超值 这种方法显然适用于常量值,但您可能希望从其他地方检索这些值。我们可以将此解决方案扩展到动态计算的值吗 对。我们可以存储函数,而不是将字符串存储为映射的值。当我们想要访问值时,我们只需要调用函数,函数可以做任何它想做的事情来计算值。新映射可能如下所示:
for(var id in values) {
if(Object.prototype.hasOwnProperty.call(values, id)) {
var element = document.getElementById(id);
if(element !== null) {
element.value = values[id];
}
}
}
var values = {
// If there's something on the page with an ID of nameDisplay, prompt the
// user for what to fill it with.
nameDisplay: function() {
return prompt("What's your name?", "");
},
// If there's an element with an ID of time on the page, fill it with the
// current time.
time: function() {
var now = new Date();
return now.toString();
}
};
我们的循环几乎不需要修改。我们需要做的就是将给出值的行更改为:
element.value = values[id]();
注意括号。现在我们正在调用一个存储在映射中的函数。在注释中,您注意到所有“做点什么”位所做的都是填充一个值
属性。我认为这是一种常见的结构
在最简单的形式中,您需要保留一个元素ID和值对的列表,以尝试将其放入这些元素中。由于元素ID是唯一的,我认为映射是合适的:
var values = {
myid1: "Hello, world! You're looking at the element with the ID of myid1.",
myid2: "I'm the element with the ID of myid2!"
};
显然,接下来的过程是通过成对循环,寻找元素。如果存在,则设置该值;否则,这不是问题:继续前进
在JavaScript中,您可以使用for
。In
循环来循环对象中的对。不过,这有点棘手,因为您只想检查自己的属性;也就是说,特定地属于该对象,并且不是从其他地方继承的属性
总之,当把它们放在一起时,你可能会得到这样的结果:
for(var id in values) {
if(Object.prototype.hasOwnProperty.call(values, id)) {
var element = document.getElementById(id);
if(element !== null) {
element.value = values[id];
}
}
}
var values = {
// If there's something on the page with an ID of nameDisplay, prompt the
// user for what to fill it with.
nameDisplay: function() {
return prompt("What's your name?", "");
},
// If there's an element with an ID of time on the page, fill it with the
// current time.
time: function() {
var now = new Date();
return now.toString();
}
};
超值
这种方法显然适用于常量值,但您可能希望从其他地方检索这些值。我们可以将此解决方案扩展到动态计算的值吗
对。我们可以存储函数,而不是将字符串存储为映射的值。当我们想要访问值时,我们只需要调用函数,函数可以做任何它想做的事情来计算值。新映射可能如下所示:
for(var id in values) {
if(Object.prototype.hasOwnProperty.call(values, id)) {
var element = document.getElementById(id);
if(element !== null) {
element.value = values[id];
}
}
}
var values = {
// If there's something on the page with an ID of nameDisplay, prompt the
// user for what to fill it with.
nameDisplay: function() {
return prompt("What's your name?", "");
},
// If there's an element with an ID of time on the page, fill it with the
// current time.
time: function() {
var now = new Date();
return now.toString();
}
};
我们的循环几乎不需要修改。我们需要做的就是将给出值的行更改为:
element.value = values[id]();
注意括号。现在,我们正在调用存储在映射中的函数。Do something(做某事)是否具有任何公共结构?否,它根据元素的不同使用不同的变量填充属性“value”。请使用实际示例解释您的问题。在下面的注释中,您说字符串每次都不同。请给出这方面的真实示例。“做点什么”是否有任何共同的结构?不,它根据元素使用不同的变量填充属性“值”。请使用真实示例解释您的问题。在下面的注释中,您说字符串每次都不同。请给出这方面的真实示例。非常感谢您的快速回答,我没有很好地解释这是我的错,但ID不是数字,它是一个字符串,每次都不一样:/因此,创建一个字符串数组并对其进行迭代。非常感谢您的快速回答,我没有很好地解释这是我的错,但ID不是数字,这是一个字符串,每一次都不一样:/因此,创建一个字符串数组并对其进行迭代。我不擅长解释,抱歉,但从技术上讲,我并不拥有添加JS代码的页面,我知道我说了“我有”,我为误解感到抱歉,非常感谢您的快速回答。:)“从技术上讲,我并不拥有这些网页”。。。。你的意思是说你不能添加新的js页面?是的,这就是我的意思。如果是这样,我看不到任何解决方案,因为你必须为每个页面修改相同的特定js。为了使其通用化,您必须添加一个所有jsp/html文件都可以引用的页面。非常感谢您的帮助,我喜欢icktoofay sama的解决方案,我将尝试一下。我不擅长解释,抱歉,但从技术上讲,我不拥有添加JS代码的页面,我知道我说了“我有”,我为误解感到抱歉,谢谢