随笔 - 20  文章 - 8  trackbacks - 0
<2007年5月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

常用链接

留言簿(1)

随笔分类

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜

在新窗口中打开链接很简单,下面的javascript即可完成功能.
window.open ("page.html", "newwindow", "height=100, width=100, top=0, left=0,toolbar=no, menubar=no, scrollbars=no,

resizable=no, location=no, status=no")

今天有个比较特别的需求,为页面中动态生成的html元素a添加响应。开始想为a元素添加onclick响应函数。可是由于犯晕老是将

href.onclick="javascript:window.open()",实验半天也没有半点进展。后来同事添加了一个函数,修改href.href="javascript:newwindow

()",可以在新窗口中打开了。由于一心想修改onclick,仔细回忆了以下javascript的模型,发现自己试图为onclick赋值的方法错了。由于

javascript中不管是变量还是函数都是对象,要想为onclick赋值,需要赋值一个函数对象。所以需要写成href.onclick=hrefOnClick。下面是

测试用的代码:
<html>
<head />
<body>
<script language="javascript">
<!--
function hrefOnClick(){
    window.open("http://google.com.cn", "", "");
}
function hrefHref(url){
    window.open(url, "", "");
}

var div;
div = document.createElement("DIV");

var href;

href = document.createElement("a");
href.style.color = "black";
href.style.fontFamily = "宋体";
href.style.fontSize  = "9pt";
href.style.backgroundColor = "#F1FF77";
href.style.textDecoration="none";
href.innerHTML = "点击这个链接在新窗口中打开链接,修改href";
href.href = "javascript:hrefHref('http://google.com.cn')";
href.target = "_top";
div.appendChild(href);

div.appendChild(document.createElement("br"));

href = document.createElement("a");
href.style.color = "black";
href.style.fontFamily = "宋体";
href.style.fontSize  = "9pt";
href.style.backgroundColor = "#F1FF77";
href.style.textDecoration="none";
href.innerHTML = "点击这个链接在新窗口中打开链接,修改onclick";
href.href = "#";
href.onclick = hrefOnClick;
div.appendChild(href);

document.body.appendChild(div);
//document.body.insertAdjacentElement("afterBegin",div);

-->
</script>
</body>
</html>
posted on 2007-05-29 15:21 卜清楚 阅读(2947) 评论(0)  编辑  收藏 所属分类: javascript

只有注册用户登录后才能发表评论。


网站导航: