用JS 怎么实现链接跳转到新窗口

JavaScript018

用JS 怎么实现链接跳转到新窗口,第1张

添加以下代码执行就可以了

<SCRIPT

LANGUAGE="JavaScript">

<!-

var

anchors

=

document.getElementById("DIV名").getElementsByTagName("a")

for(var

i=0i<anchors.lengthi++)

{var

anchor

=

anchors[i]anchor.target="_blank"}

//–>

</SCRIPT>

注解:target="_blank"

这个代码就是新窗口打开效果。

点击按钮怎么跳转到另外一个页面呢?我们在网站制作中可能是需要的,因为有时我们需要做这样的效果,尤其是将按钮做成一个图片,而点击图片要跳转到新的页面时,怎么做到呢?

这样的效果可以:onclick="window.location='新页面'"

来实现。

1.在原来的窗体中直接跳转用

代码如下

window.location.href="你所要跳转的页面"

2、在新窗体中打开页面用:

代码如下

window.open('你所要跳转的页面')

window.history.back(-1)返回上一页

代码如下

<input

type="submit"

name="Submit"

value="同意"

onclick=window.open(http://www.jb51.net/)>

如果要在点击按钮提交时验证输入款是否填入了内容,要怎么做呢?当用户名输入或者其它的为空的时候,点击按钮不提交,可以按下列的方法做。

代码如下

复制代码

代码如下:

<input

type="submit"

name="submit"

onclick="open()">

<script

language=javascript>

fuction

open(){

if(!document.form_name.username.value)

{

alert("请输入用户名!")

document.form_name.username.focus()

return

false

}else

document.form_name.action="aaa.htm"

}

</script>