怎么在2个html页面间传值

html-css05

怎么在2个html页面间传值,第1张

html是静态页面,可以使用url链接传值,比如a.html和b.html两个页面

a.html中有一个链接

1

<a href="b.html?x=2&y=3">进入b.html</a>

可以使用到js,如下:

a.htm:

1

2

3

4

<form action="b.htm" >

<input name="q" type="text" value="" />

<input type="submit" value="提交" id="" />

</form>

b.htm

<html>

<body>

<div id="qbox"></div>

<script type="text/javascript">

function getArgs() {

var args = {}

var query = location.search.substring(1)

// Get query string

var pairs = query.split("&")

// Break at ampersand

for(var i = 0i <pairs.lengthi++) {

var pos = pairs[i].indexOf('=')

// Look for "name=value"

if (pos == -1) continue

// If not found, skip

var argname = pairs[i].substring(0,pos)// Extract the name

var value = pairs[i].substring(pos+1)// Extract the value

value = decodeURIComponent(value)// Decode it, if needed

args[argname] = value

// Store as a property

}

return args// Return the object

}

var str =getArgs()

alert(str['q'])//和input的name对应取值,

document.getElementById("qbox").innerHTML = str['q']//然后赋值给DIV

</script>

</body>

</html>

希望能帮到你哦!

dojo实现两个html页面之间传值为222。

HTML是一种规范,一种标准,它通过标记符号来标记要显示的网页中的各个部分。网页文件本身是一种文本文件,通过在文本文件中添加标记符,可以告诉浏览器如何显示其中的内容(如:文字如何处理,画面如何安排,图片如何显示等)。浏览器按顺序阅读网页文件,然后根据标记符解释和显示其标记的内容,对书写出错的标记将不指出其错误,且不停止其解释执行过程,编制者只能通过显示效果来分析出错原因和出错部位。但需要注意的是,对于不同的浏览器,对同一标记符可能会有不完全相同的解释,因而可能会有不同的显示效果。

1、使用表单传递数据

两个html页面,a.html,b.html想。把a.html中的表单form中的id和pwd传到b.html中

<form action="b.html" method="post" name="formName"></form>

要用form表单配合后台语言来传,发送类型POST和GET看你需求要换。

2、使用JS传递接收数据

<html1 ><input type="text" value="nihao" id="text"></html>

<html2></html>

如何用JS把HTML1中的text属性值nihao 传递给html2  在HTML2中有怎样用JS接收传递过来的数据并显示。

<html1><a href="html2.html?word=nihao"><a>

<html2>

js代码:

var str=location.href.search//取地址参数部分

word = str.sbustr(str.indexOf('=')+1)

word就可以获得地址中传递的参数了;如果有多个参数可以用split函数

3、两个纯Html之间的传值实例

index1.htm页面

<HTML>

    <HEAD>

    <TITLE> New Document </TITLE>//标题

        <SCRIPT LANGUAGE="JavaScript"> //调用JavaScript方法   

        function show(){       

         var result = document.getElementByIdx("name").value  //获取值.     

          location.href="index2.htm?name="+result  //  链接跳转

                      }

        </SCRIPT>

          <style>.input7 {color: #999width:145pxheight:20pxborder: 1px solid #CCCCCC font-size:12pxbackground-color: #fff}//css样式

          </style>

    </HEAD>

    <BODY>

    <input type="text" id="name" class="input7"><input type="button" value="OK" onclick="show()"/>//输出

    </BODY>

</HTML>

index2页面:

<HTML>

<HEAD>

<TITLE> New Document </TITLE>

<SCRIPT LANGUAGE="JavaScript">

function getvalue(name){    

    var str=window.location.search   //location.search是从当前URL的?号开始的字符串     

    if (str.indexOf(name)!=-1)//判断是否收到值

     {                

     var pos_start=str.indexOf(name)+name.length+1  //解析获取值   

     var pos_end=str.indexOf("&",pos_start)        

     if (pos_end==-1){           

      alert( str.substring(pos_start))  //输出      

                      }

     else{           

      alert("没有此值~~")    

          }  

      }

</SCRIPT>

</HEAD>

<BODY>

<input type="button" onclick="getvalue('show')" value="GetValue"/>

</BODY>

</HTML>