简体   繁体   English

在另一个HTML页面中更改div

[英]Change the div's in another html page

still learning some javascript here, got done other things but now the final and most important part of it. 还在这里学习一些javascript,做了其他事情,但现在是它的最后也是最重要的部分。

I have two html pages - one of which uses javascript to dynamically add text-fields (and to remove them of course) (genmain.html) and the other one where the text field input should go(table.html). 我有两个html页面-其中一个使用javascript动态添加文本字段(当然也要删除它们)(genmain.html),另一页面使用文本字段输入(table.html)。

So i have already created a function to retrieve the array of values. 所以我已经创建了一个函数来检索值数组。

function getElementArray(divName){
   var names = document.getElementsByName("namefield");
}

The variable names is an array and it has all the values from fields. 变量名是一个数组,它具有字段中的所有值。

The problem is I would like to set these values from array to the values of another div on the page. 问题是我想将数组中的这些值设置为页面上另一个div的值。 After some searching i understood that it could be done with 'id'-s but i'm not that sure and don't completely understand how. 经过一番搜索,我知道可以用'id'-s来完成,但我不确定,也不完全知道如何做。

Let's say i have a lot of div's on another page (table.html) but some of them have id="MAIN". 假设我在另一页(table.html)上有很多div,但是其中一些具有id =“ MAIN”。 I would like to change the value inside of the div 我想在div内更改值

For example 例如

<div id="MAIN">THIS PART I WANT TO CHANGE</div>

Javascript is not part of my school system and i've done CodeAcademy tutorials and that's the most i've got about this, I hope you guys can help with my issue. Javascript不是我学校系统的一部分,我已经完成了CodeAcademy教程,这是我所获得的最多的知识,我希望你们能为我的问题提供帮助。

The variable names is an array and it has all the values from fields. 变量名是一个数组,它具有字段中的所有值。

function getElementArray(divName){
   var names = document.getElementsByName("namefield");
}

Nope, you've only got reference to the elements here. 不,您仅在此处引用了元素。 You've not got the value yet. 您还没有价值。

You can get the values by iterating through the names Nodelist array and use names[i].value 您可以通过遍历名称Nodelist数组并使用names[i].value来获取值。

The problem is I would like to set these values from array to the values of another div on the page 问题是我想将数组中的这些值设置为页面上另一个div的值

If it's going to be in same page, then use innerHTML or textContent property of the DOM to assign the value. 如果要在同一页面中,则使用DOM的innerHTMLtextContent属性分配值。

document.getElementById("MAIN").textContent= names[1].value;

Just for demo purpose am using names[1] here so it will load the second input value. 出于演示目的,我在此处使用names[1] ,这样它将加载第二个输入值。

Let's say i have a lot of div's on another page (table.html) but some of them have id="MAIN". 假设我在另一页(table.html)上有很多div,但是其中一些具有id =“ MAIN”。 I would like to change the value inside of the div 我想在div内更改值

Once you move to another page, the javascript state will be lost. 一旦您移至另一个页面,则javascript状态将丢失。 So you wont have access to names inside that page. 因此,您将无法访问该页面内的names

Either you must store the values into localStorage and retrieve in next page. 您必须将这些值存储到localStorage中并在下一页中进行检索。

Else add the values to query string of your URL and retrive it there. 否则,将值添加到您的URL的query字符串中,然后在其中进行检索。

Edit: Update based on comments 编辑:根据评论更新

Let us assume you have var names = document.getElementsByName("namefield"); 让我们假设您有var names = document.getElementsByName("namefield"); so to store the values inside localStorage. 以便将值存储在localStorage中。

var myValues = [],
    names = document.getElementsByName("namefield");
    for(var i = 0; i < names.length; i++) {
        myValues.push(names[i].value);
    } 
    localStorage.myValues = JSON.stringify(myValues);

Now if your next page, Iinside window.onload event: 现在,如果您的下一页是Iinside window.onload事件:

   window.onload = function() {
       var myValues = localStorage.getItem("myValues") ? JSON.parse(localStorage.getItem("myValues")) : [],
           divElements = document.querySelectorAll("#MAIN");
       for(var i =0; i < myValues.length; i++) {
           divElements[i].textContent = myValues[i];
       }
   }

If you want to set or change the contents of an element, you can use the innerHTML property. 如果要设置或更改元素的内容,则可以使用innerHTML属性。

So in your case, document.getElementById("MAIN").innerHTML = "Whatever you want"; 因此,在您的情况下, document.getElementById("MAIN").innerHTML = "Whatever you want";

For the record, names in your example technically isn't an array, but a NodeList . 作为记录,您的示例中的names从技术上讲不是数组,而是NodeList See https://developer.mozilla.org/en/docs/Web/API/NodeList#Why_is_NodeList_not_an_Array.3F . 参见https://developer.mozilla.org/en/docs/Web/API/NodeList#Why_is_NodeList_not_an_Array.3F

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM