我有一个a标签,里边有href属性,有onclick方法,
标签a是不能改变了,我想达到的目标是:
在单击标签a的时候,触发onclick事件,弹出一个对话框(已经实现),根据用户填入的内容,来添加转向页面的参数,
?dd=dd&cc=ss....等等,用户可以关闭对话框,不转向其他页面,回到原来的页面如此
不知道说的明白不明白,这个onclick事件和标签a页面转向二者有什么冲突???
标签a是不能改变了,我想达到的目标是:
在单击标签a的时候,触发onclick事件,弹出一个对话框(已经实现),根据用户填入的内容,来添加转向页面的参数,
?dd=dd&cc=ss....等等,用户可以关闭对话框,不转向其他页面,回到原来的页面如此
不知道说的明白不明白,这个onclick事件和标签a页面转向二者有什么冲突???
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
</head><body>
<a id="test" href="http://www.google.cn" target="_blank">google</a>
</body>
</html>
<script type="text/javascript">
var _test = document.getElementById("test");
_test.onclick = function(){
if(confirm("是否打开这个页面?")){
window.open(_test.href);
}
return false; //关键是这个。加了它可以取消点击事件。
}
</script>
如果a上有其他事件,你完全可以把a标签的onclick事件整合到一起去。
最好把你的代码贴上来,或者写个示例代码,这样问题比较清晰。
<!-- a 上原本有另一个onclick事件: -->
<a id="test" href="http://www.google.cn" onclick="alert('test');" target="_blank">google</a><script type="text/javascript">
var _test = document.getElementById("test");
var fun = _test.onclick; //获取a上原先的点击事件。_test.onclick = function(){ //给a绑定新的事件。
if(confirm("是否打开这个页面?")){ //你也可以不用confirm,转而去执行其他程序。比如显示你的对话框。
fun(); //执行原先的事件。
window.open(_test.href); //执行打开窗口,不过不想弹出,也可以改变浏览器URL。
}
return false; //最后关键是这个。加了它可以取消点击事件。
}</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312" />
<title>test</title>
<script type="text/javascript">
var m_URL='';
function addPara(url){
m_URL=url;
window.open("child.htm","t")
}
</script></head><body>
<a id="test" href="#" onclick="javascript:addPara('aaa.htm')">转去AAA页面</a>
</body>
</html>弹出窗口:child.htm
<script>
function transferto(){
var url=window.opener.m_URL+document.getElementById('para').value; window.opener.document.location.href=url;
window.close;
}
</script>
<body>
请填写参数
<input type=text id=para name=para value="?id=123" size=50px;/>
<button onclick="javacript:transferto();">确定</button>
<button onclick="javacript:window.close();">取消</button>
<br><br>
</body>
<input id="dd" type="text" value="">
<input type="button" value="ok" onclick="ok();">
<input type="button" value="cancel" onclick="cancel();">
</div> <!-- 模拟下对话框 -->
<a id="atest" href="a.html" onclick="test(this);" lang="0">click me</a>
var buf;
function test(obj){
if(!buf){
buf = obj.href;
}
if(obj.lang=="0"){
document.getElementById("par").style.display = "block";
obj.href = "javascript:;";
} else{
obj.lang = "0";
location.href = obj.href;
} } function ok(){ document.getElementById("atest").href = buf + "?dd=" + document.getElementById("dd").value;
document.getElementById("atest").lang = "1";
document.getElementById("par").style.display = "none";
test(document.getElementById("atest"));
} function cancel(){
document.getElementById("par").style.display = "none";
document.getElementById("atest").lang = "0";
document.getElementById("atest").href = buf;
buf = null;
}
<script>
function doThing(){
//...
return false;
}
</script>
只能用#作为占位符。
<script>
function cancelDefaultEvent(event){
if (event.preventDefault) event.preventDefault();
else event.returnValue = false;
}function doThing(event){
//...
cancelDefaultEvent(event);
}
</script>
preventDefault属性和方法
preventDefault()我查了下并不存在,难道是我的DHTML文档落后?还是为了兼容其他的浏览器??
true Default. Value from the event is returned.
false Default action of the event on the source object is canceled.
直接:
event.returnValue=false;//
<a href="www.google.com" onclick="MyClick(Event)">var gEvent;
function MyClick(objEvent){
.....
gEvent=objEvent;
}
function another(){
//在这里使用gEvent,这样不行吧?????????????????}
同意
retrun false即可
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312" />
<title>test</title>
<script type="text/javascript">
var m_URL='';
function addPara(url){
m_URL=url;
window.open("child.htm","t")
}
</script></head><body>
<a id="test" href="#" onclick="javascript:addPara('aaa.htm')">转去AAA页面</a>
</body>
</html>
<a href="javascript:void(0)" onclick="GetHref()"></a>
function GetHref(){
//弹出窗口的方法返回href
if(href) location.href=href;
}
<script>
function doThing(){
if(confirm("是否打开这个页面?")){
return true
}else{
(S=prompt("请输入其它页网址:", ""))
!=null && S!="" && window.open(S);
return false
}
}
</script>
target="_blank">The Demo</a>
<script>
function doThing(e){
var e = e || event.srcElement;
var url = e.href.replace(/[^\?]+$/g,"");//alert(url);
var str = prompt("请输入要转向页面参数:", "fid=1&page=2");
str != null && str != "" && window.open(url + str);
return false
}
</script>