Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何访问一堆?_Javascript - Fatal编程技术网

Javascript 如何访问一堆?

Javascript 如何访问一堆?,javascript,Javascript,我有一个文本列表,我想更改它们的内部HTML。若我有数千个li标记,其数据来自数据库,那个么如何通过javascript实现这一点 <div> <ul> <li>a</li> <li>as</li> <li>asd</li> <li>asds</li> <li>asdsa</li> <li>asdsad</li> <li&g

我有一个文本列表,我想更改它们的内部HTML。若我有数千个li标记,其数据来自数据库,那个么如何通过javascript实现这一点

<div>
<ul>
<li>a</li>
<li>as</li>
<li>asd</li>
<li>asds</li>
<li>asdsa</li>
<li>asdsad</li>
<li>asdsadz</li>
<li>asdsadzc</li>
....
.....
</ul>
</div>
function a(){
    var parent = document.getElementById("z");
    var i = 0;
    for(i = 0; i <= parent.children.length; i++){
        if(parent.children[i].tagName == "LI"){
            if(i%2!=0){
                parent.children[i].innerHTML="ok";
            }
        }
    }
}
document.onload=a(); // this didn't work. so I called the function in body tag instead of that.
<body onload="a();">
-谢谢

function a(){
    var parent = document.getElementById("z");
    var i = 0;
    for(i = 0; i <= parent.children.length; i++){
        if(parent.children[i].tagName == "LI"){
            if(i%2!=0){
                parent.children[i].innerHTML="ok";
            }
        }
    }
}
document.onload=a(); // this didn't work. so I called the function in body tag instead of that.
<body onload="a();">
更新 正在使用的JS代码:

function a(){
    var parent = document.getElementById("z");
    var i = 0;
    for(i = 0; i <= parent.children.length; i++){
        if(parent.children[i].tagName == "LI"){
            if(i%2!=0){
                parent.children[i].innerHTML="ok";
            }
        }
    }
}
document.onload=a(); // this didn't work. so I called the function in body tag instead of that.
<body onload="a();">

考虑到您希望使用jQuery的先决条件(不太牵强),您可以选择它们并使用每个先决条件对它们进行迭代

function a(){
    var parent = document.getElementById("z");
    var i = 0;
    for(i = 0; i <= parent.children.length; i++){
        if(parent.children[i].tagName == "LI"){
            if(i%2!=0){
                parent.children[i].innerHTML="ok";
            }
        }
    }
}
document.onload=a(); // this didn't work. so I called the function in body tag instead of that.
<body onload="a();">
$("li").each( 
function() { $(this).html("changed content"); }
);
如果您不使用jQuery,那么使用js库来帮助您解决古怪的dom可能不是一个坏主意

function a(){
    var parent = document.getElementById("z");
    var i = 0;
    for(i = 0; i <= parent.children.length; i++){
        if(parent.children[i].tagName == "LI"){
            if(i%2!=0){
                parent.children[i].innerHTML="ok";
            }
        }
    }
}
document.onload=a(); // this didn't work. so I called the function in body tag instead of that.
<body onload="a();">
总体思路

function a(){
    var parent = document.getElementById("z");
    var i = 0;
    for(i = 0; i <= parent.children.length; i++){
        if(parent.children[i].tagName == "LI"){
            if(i%2!=0){
                parent.children[i].innerHTML="ok";
            }
        }
    }
}
document.onload=a(); // this didn't work. so I called the function in body tag instead of that.
<body onload="a();">
选择节点 迭代并更改html
总是一样的

如果您希望使用jQuery的前提条件不是那么牵强,那么您可以选择它们并用它们进行迭代

function a(){
    var parent = document.getElementById("z");
    var i = 0;
    for(i = 0; i <= parent.children.length; i++){
        if(parent.children[i].tagName == "LI"){
            if(i%2!=0){
                parent.children[i].innerHTML="ok";
            }
        }
    }
}
document.onload=a(); // this didn't work. so I called the function in body tag instead of that.
<body onload="a();">
$("li").each( 
function() { $(this).html("changed content"); }
);
如果您不使用jQuery,那么使用js库来帮助您解决古怪的dom可能不是一个坏主意

function a(){
    var parent = document.getElementById("z");
    var i = 0;
    for(i = 0; i <= parent.children.length; i++){
        if(parent.children[i].tagName == "LI"){
            if(i%2!=0){
                parent.children[i].innerHTML="ok";
            }
        }
    }
}
document.onload=a(); // this didn't work. so I called the function in body tag instead of that.
<body onload="a();">
总体思路

function a(){
    var parent = document.getElementById("z");
    var i = 0;
    for(i = 0; i <= parent.children.length; i++){
        if(parent.children[i].tagName == "LI"){
            if(i%2!=0){
                parent.children[i].innerHTML="ok";
            }
        }
    }
}
document.onload=a(); // this didn't work. so I called the function in body tag instead of that.
<body onload="a();">
选择节点 迭代并更改html 总是一样的

您是否尝试过使用getElementsByTagName?像这样的歌曲会帮助你找到你想要使用的元素

function a(){
    var parent = document.getElementById("z");
    var i = 0;
    for(i = 0; i <= parent.children.length; i++){
        if(parent.children[i].tagName == "LI"){
            if(i%2!=0){
                parent.children[i].innerHTML="ok";
            }
        }
    }
}
document.onload=a(); // this didn't work. so I called the function in body tag instead of that.
<body onload="a();">
编辑

function a(){
    var parent = document.getElementById("z");
    var i = 0;
    for(i = 0; i <= parent.children.length; i++){
        if(parent.children[i].tagName == "LI"){
            if(i%2!=0){
                parent.children[i].innerHTML="ok";
            }
        }
    }
}
document.onload=a(); // this didn't work. so I called the function in body tag instead of that.
<body onload="a();">
如果您可以为保存要处理的li的UL元素提供Id,您可以执行以下操作:

function a(){
    var parent = document.getElementById("z");
    var i = 0;
    for(i = 0; i <= parent.children.length; i++){
        if(parent.children[i].tagName == "LI"){
            if(i%2!=0){
                parent.children[i].innerHTML="ok";
            }
        }
    }
}
document.onload=a(); // this didn't work. so I called the function in body tag instead of that.
<body onload="a();">
var parent = document.getElementById("yourID");
var i = 0;
for(i = 0; i < parent.children.length; i++){ 
  if(parent.children[i].tagName == "LI") { 
     //do what you want...
   }
}
对于这个:window.onload=a

function a(){
    var parent = document.getElementById("z");
    var i = 0;
    for(i = 0; i <= parent.children.length; i++){
        if(parent.children[i].tagName == "LI"){
            if(i%2!=0){
                parent.children[i].innerHTML="ok";
            }
        }
    }
}
document.onload=a(); // this didn't work. so I called the function in body tag instead of that.
<body onload="a();">
这将使您的函数在onLoad事件上执行。请注意,可能存在一些交叉浏览器不兼容的情况,我建议研究一下如何以交叉浏览器的方式在onload事件上执行函数,或者只是将其添加到body标记中:

function a(){
    var parent = document.getElementById("z");
    var i = 0;
    for(i = 0; i <= parent.children.length; i++){
        if(parent.children[i].tagName == "LI"){
            if(i%2!=0){
                parent.children[i].innerHTML="ok";
            }
        }
    }
}
document.onload=a(); // this didn't work. so I called the function in body tag instead of that.
<body onload="a();">
<body onload="a();">
您是否尝试过使用getElementsByTagName?像这样的歌曲会帮助你找到你想要使用的元素

function a(){
    var parent = document.getElementById("z");
    var i = 0;
    for(i = 0; i <= parent.children.length; i++){
        if(parent.children[i].tagName == "LI"){
            if(i%2!=0){
                parent.children[i].innerHTML="ok";
            }
        }
    }
}
document.onload=a(); // this didn't work. so I called the function in body tag instead of that.
<body onload="a();">
编辑

function a(){
    var parent = document.getElementById("z");
    var i = 0;
    for(i = 0; i <= parent.children.length; i++){
        if(parent.children[i].tagName == "LI"){
            if(i%2!=0){
                parent.children[i].innerHTML="ok";
            }
        }
    }
}
document.onload=a(); // this didn't work. so I called the function in body tag instead of that.
<body onload="a();">
如果您可以为保存要处理的li的UL元素提供Id,您可以执行以下操作:

function a(){
    var parent = document.getElementById("z");
    var i = 0;
    for(i = 0; i <= parent.children.length; i++){
        if(parent.children[i].tagName == "LI"){
            if(i%2!=0){
                parent.children[i].innerHTML="ok";
            }
        }
    }
}
document.onload=a(); // this didn't work. so I called the function in body tag instead of that.
<body onload="a();">
var parent = document.getElementById("yourID");
var i = 0;
for(i = 0; i < parent.children.length; i++){ 
  if(parent.children[i].tagName == "LI") { 
     //do what you want...
   }
}
对于这个:window.onload=a

function a(){
    var parent = document.getElementById("z");
    var i = 0;
    for(i = 0; i <= parent.children.length; i++){
        if(parent.children[i].tagName == "LI"){
            if(i%2!=0){
                parent.children[i].innerHTML="ok";
            }
        }
    }
}
document.onload=a(); // this didn't work. so I called the function in body tag instead of that.
<body onload="a();">
这将使您的函数在onLoad事件上执行。请注意,可能存在一些交叉浏览器不兼容的情况,我建议研究一下如何以交叉浏览器的方式在onload事件上执行函数,或者只是将其添加到body标记中:

function a(){
    var parent = document.getElementById("z");
    var i = 0;
    for(i = 0; i <= parent.children.length; i++){
        if(parent.children[i].tagName == "LI"){
            if(i%2!=0){
                parent.children[i].innerHTML="ok";
            }
        }
    }
}
document.onload=a(); // this didn't work. so I called the function in body tag instead of that.
<body onload="a();">
<body onload="a();">


你选择它们并对其进行迭代:。您使用的服务器端语言是什么?PHP。网你说它们来自数据库?这是ajax响应吗?你是否需要在页面呈现后更改li的html,或者我们可以使用服务器端脚本格式化li的内容?如果我尝试getElementById,那么很难为每个li标记指定id,我无法尝试getElementByTagName,因为页面上可能有其他li标记。我正在使用php从数据库中绘制数据。是否可以为所有li标记赋予相同的id,并获取id li的长度?可能,但不好:id暗示唯一性您。。。选择它们并对其进行迭代:。您使用的服务器端语言是什么?PHP。网你说它们来自数据库?这是ajax响应吗?你是否需要在页面呈现后更改li的html,或者我们可以使用服务器端脚本格式化li的内容?如果我尝试getElementById,那么很难为每个li标记指定id,我无法尝试getElementByTagName,因为页面上可能有其他li标记。我正在使用php从数据库中绘制数据。是否可以为所有li标记赋予相同的id,并获取id li的长度?可能,但不好:id表示唯一性她没有指定是否使用任何类型的库。。。因此,$实际上可能会失败。没有提到jQuery,但页面上可能会有其他li标记。是否可以为所有li标记提供相同的id并获取id li的长度?@user1844626:然后您应该更新选择器$li。如果只有li在id为thisid的div中,它就变成:$divthisid li例如,一切都是可能的。如果你真的打算在dom html结构上使用Either vanilla javascript或jquery,你应该努力研究一下,这会有回报的。。。因此,$实际上可能会失败。没有提到jQuery,但页面上可能会有其他li标记。是否可以为所有li标记提供相同的id并获取id li的长度?@user1844626:然后您应该更新选择器$li。如果只有li在id为thisid的div中,它就变成:$divthisid li例如,一切都是可能的。如果你真的打算在dom html结构上使用Either vanilla javascript或jquery,你应该做一些努力来研究它,它会有回报的,如果您想要交叉浏览,对不起,我不能尝试getElementsByTagName,因为页面上可能有其他li标记。是否可以为所有li标记赋予相同的id,并获取id li的长度?@Peter Yes为li的标记赋予类或为其父标记赋予id,其工作方式与IMHO大致相同。@user1844626您需要检查标记名是否为li,我想你可能在标签之间找到了文本节点,如果您想要它,对不起,我不能尝试getElementsByTagName,因为可能有b
e页面上的其他li标记。是否可以为所有li标记指定相同的id并获取id li的长度?@Peter Yes为li标记指定一个类或为其父标记指定一个id,其工作方式与IMHO几乎相同。@user1844626您需要检查标记名是否为li,我想您可能正在查找标记之间的文本节点。。。