[英]Can jQuery shorten a series of innerHTML statements?
On my sites, I have two functions with several consecutive javascript innerHTML statements like so: 在我的网站上,我有两个连续几个javascript innerHTML语句的函数,如下所示:
function myFunction(a, b, c, d, e, f, g) {
//Lots of code then
document.getElementById("id1").innerHTML = a;
document.getElementById("id2").innerHTML = b;
//Five more lines of this through parameter g
}
This first function has seven lines for seven parameters, and the second function has 16 lines for 16 parameters. 此第一个函数有七个参数行的七行,第二个函数有16个参数行的16行。 I understand (if I use jQuery and I name each id with a numeric convention, see below) I can shorten this code as follows (function 2 as an example):
我了解(如果我使用jQuery并使用数字约定命名每个id,请参见下文),我可以按如下方式简化此代码(以功能2为例):
function myFunction2(a, b, c, .......p) {
//Lots of code then
var z, myArray = [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p];
for (z in myArray) {
$("#id" + (z + 1)).html(myArray[z]);//used z to avoid name collision with a through p
}
}
Is there any way I can use jQuery to do something like the following? 有什么方法可以使用jQuery来执行以下操作?
function myFunction2(a, b, c, .......p) {
//Lots of code then
$("#id1, #id2, ....... #id16").html(a, b, c.......p);
}
My thinking is that each id would be matched with each parameter within the html() statement. 我的想法是,每个id将与html()语句中的每个参数匹配。 I have not found any examples of this in my searches.
我在搜索中没有找到任何此类示例。 I don't think this code (if it is possible) would even necessarily be better than the for loop above but it saves a couple of lines, hence the question.
我不认为此代码(如果可能)甚至不一定比上面的for循环更好,但它节省了几行,因此是问题所在。 Thanks for any answers
谢谢你的回答
If your IDs really are numerically named, you don't need jQuery to shorten things: 如果您的ID确实是用数字命名的,则不需要jQuery来简化代码:
function myFunction2(/* don't need the parameter list */) {
//Lots of code then
for ( var i = 0; i < arguments.length; ++i )
{
document.getElementById( 'id' + (i + 1) ).innerHTML = arguments[i];
}
}
Or pass in a prefix to use it for different sets of IDs, all depending on the number of parameters passed: 或传入前缀以将其用于不同的ID集,这些都取决于传递的参数数量:
function myFunction2(prefix) {
//Lots of code then
for ( var i = 1; i < arguments.length; ++i )
{
document.getElementById( prefix + i ).innerHTML = arguments[i];
}
}
myFunction2('id', "a", "b", "c"); // sets #id1, #id2, #id3 to a, b, c
myFunction2('other", "x", "y"); // sets #other1, #other2 to x, y
function thing() {
$.each(arguments, function(n, val) {
$('#id'+ (n+1)).html(val);
});
}
then 然后
thing('first', 'second', 'third', .... 'millionth')
nope 不
selector $('#id1, #id2, #id3') returns an array of 3 jQuery objects 选择器$('#id1,#id2,#id3')返回3个jQuery对象的数组
calling on them .html(...) applies that function to all selected items 调用它们.html(...)将该函数应用于所有选定项
edit: 编辑:
also know that 也知道
var f = function() { console.log(arguments); };
when you call it 当你叫它
f(1,2,3,'a','b','c');
it will return 它将返回
[1,2,3,'a','b','c']
so you can modify your function2: 因此您可以修改功能2:
function myFunction2() {
// lots of code then
for ( var z in arguments ) {
$( "#id" + (z + 1) ).html( arguments[z] ); // used z to avoid name collision with a through p
}
}
good luck! 祝好运!
No, unfortunately the html method only accepts zero or one parameter. 不,不幸的是,html方法仅接受零或一个参数。 You can write a selector which will target various different DOM elements, but they will all be passed the same parameters from your html() call.
您可以编写一个选择器,该选择器将以各种不同的DOM元素为目标,但是它们都会从html()调用中传递相同的参数。 If you want to get a little creative, you can pass html() a function whose logic can depend on some property or attribute of the parent element, but that's not going to save you many keystrokes in the end.
如果您想获得一点创意,则可以传递html()一个函数,该函数的逻辑可以取决于父元素的某些属性或属性,但这最终不会为您节省很多按键。
My solution is similar to @PaulRoub's but in this function you can pass the starting index ( demo ): 我的解决方案类似于@PaulRoub的解决方案,但是在此功能中,您可以传递起始索引( demo ):
function myFunction(start) {
var i,
arg = arguments,
len = arg.length;
for (i = 1; i < len; i++) {
$("#id" + (start + i)).html(arg[i]);
}
}
myFunction( 1, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.