我有一个a标签,里边有href属性,有onclick方法,
标签a是不能改变了,我想达到的目标是:

在单击标签a的时候,触发onclick事件,弹出一个对话框(已经实现),根据用户填入的内容,来添加转向页面的参数,
?dd=dd&cc=ss....等等,用户可以关闭对话框,不转向其他页面,回到原来的页面如此
不知道说的明白不明白,这个onclick事件和标签a页面转向二者有什么冲突???

解决方案 »

  1.   

    初始化的时候不给标签的href属性赋值,在onclick事件中在符合条件的模块再给标签的href赋值
      

  2.   

    怎么没冲突,只要标签的href不为空,在onclick的时候不管你做什么操作都会跳转到href指定的url中
      

  3.   

    就是这个a标签应该是先执行onclick事件,再转向href属性指向的页面。
      

  4.   


    <!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>
      

  5.   

    不知道我说的是不是你想要的。
    如果a上有其他事件,你完全可以把a标签的onclick事件整合到一起去。
    最好把你的代码贴上来,或者写个示例代码,这样问题比较清晰。
      

  6.   


    <!--  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>
      

  7.   

    主页面:parent.htm<!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=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>
      

  8.   

    a.html<div id="par" style="display:none">
             <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;
               }
      

  9.   

    onclick比href的优先级高,会先执行,要阻止页面跳转可以在onclick添加return false来阻止默认的事件发生<a href="#" onclick="return doThing();">demo</a>
    <script>
    function doThing(){
      //...
      return false;
    }
    </script>
      

  10.   

    12楼也是一种解决方法,你可以把href用"#"作占位符,得到参数后再和一个常量(本来在href中哪个网)组合起来用js给a连接赋值即可
      

  11.   

    我试过return false 不起作用,
    只能用#作为占位符。
      

  12.   

    绑定在a上的事件,一定要stopEvent.可以参见:http://www.easyui.org.cn/#stopEvent
      

  13.   

    <a href="http://g.cn" onclick="doThing(event);">demo</a>
    <script>
    function cancelDefaultEvent(event){
      if (event.preventDefault) event.preventDefault();
      else event.returnValue = false;
    }function doThing(event){
      //...
      cancelDefaultEvent(event);
    }
    </script>
      

  14.   

    这个可以阻止,a标签的事件发生,但是你的代码里边的
    preventDefault属性和方法
    preventDefault()我查了下并不存在,难道是我的DHTML文档落后?还是为了兼容其他的浏览器??
      

  15.   

    是event对象的returnvalue属性
    true Default. Value from the event is returned. 
    false Default action of the event on the source object is canceled. 
    直接:
    event.returnValue=false;//
      

  16.   

    还有一个疑问:
    <a href="www.google.com" onclick="MyClick(Event)">var gEvent;
    function MyClick(objEvent){
         .....
         gEvent=objEvent;
    }
    function another(){
        //在这里使用gEvent,这样不行吧?????????????????}
      

  17.   


    同意
    retrun false即可
      

  18.   

    <!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=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>
      

  19.   


    <a href="javascript:void(0)" onclick="GetHref()"></a>
    function GetHref(){
      //弹出窗口的方法返回href
      if(href) location.href=href;
    }
      

  20.   

    trying ......<a href="http://www.baidu.com" onclick="return doThing()" target="_blank">百度首页</a>
    <script>
    function doThing(){
        if(confirm("是否打开这个页面?")){
           return true
        }else{
           (S=prompt("请输入其它页网址:", ""))
           !=null && S!="" && window.open(S);
           return false
        }
    }
    </script>
      

  21.   

    没有冲突onclick中  等待弹出框的值 代码会等待的可以判断取消或确定
      

  22.   

    再给一个符合LZ题意的,自己去兼容。<a href="http://bbs.51js.com/forumdisplay.php?fid=1&page=1" onclick="return doThing()" 
    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>