如何在同一时间调用onclick和href

时间:2013-12-26 12:08:47

标签: java javascript jquery html

  boilerPalte.activityStream = 
                            "<div class='socvid-aspect-ratio-container'>"+
                                "<div onclick='com.ivb.module.home.pics.showDialogBox(\"{%=nodeId%}\",\"{%=classNameId%}\")'>"+
                                    "<a href = {%=centerUrl%}>"+
                                    "<img src='{%=imageUrl%}'>"+
                                    "</a>"+
                                "</div>"+
                            "</div>";

我希望showDialogBox函数在我单击div并同时执行href时执行。在上面的代码中onclick不起作用,只有一个href工作。 我想要一个ajax请求发生(并且可能实际上是成功的)然后让浏览器继续并找到锚标记中指定的链接

7 个答案:

答案 0 :(得分:2)

在onclick函数中可以说clickme,在成功完成ajax请求后写下你想要重定向的页面的位置。

例如。

function clickme()
{
$.get("service",{data},function(){
 location.href="www.abc.com";
});
}

答案 1 :(得分:1)

从div调用onclick并使用location.href

从函数重定向到特定页面
boilerPalte.activityStream = 
                            "<div class='socvid-aspect-ratio-container'>"+
                                "<div onclick='return com.ivb.module.home.pics.showDialogBox(\"{%=nodeId%}\",\"{%=classNameId%}\",\"{%=centerUrl%}\")'>"+
                                    "<img src='{%=imageUrl%}'>"+
                                    "</div>"+
                            "</div>";
showDialogBox中的

使用location.href

答案 2 :(得分:0)

据我所知,您可以执行以下操作。

<div href="your href here" onclick="and your function">

或者我没有正确理解你的问题?

答案 3 :(得分:0)

这对你有用:

com.ivb.module.home.pics.showDialogBox = function(nodeId, classNameId, obj) {

    // something ...

    var href = $(obj).find('a').data('href');
    makeAjaxRequest().success(function() {
        location.href = href;
    });
};

HTML:

 <div onclick='com.ivb.module.home.pics.showDialogBox(\"{%=nodeId%}\",\"{%=classNameId%}\", this)'>
     <a data-href={%=centerUrl%}>
         <img src='{%=imageUrl%}'>
     </a>
 </div>

使用data-href代替href,您无需阻止链接上的默认事件。如果您想使用href,请确保在此链接上e.preventDefault

答案 4 :(得分:0)

Java脚本:

function func_search()
                {

                        alert("Enter Search Criteria...");

                        document.location.href="search?srcstring=" + srchdata;  

                }

HTML:

<a onclick="func_search();"><img src="images/srch.png"  title="" /></a>

答案 5 :(得分:0)

看看这!!     [http://jsfiddle.net/Y4qsp/]

它可能对你有所帮助!给你的标签一个id,在那个id上调用onClick函数,并从click函数调用你的函数。

答案 6 :(得分:0)

你可以做的最简单的事情是(在jQuery中):

<div style="cursor:pointer; width: 100%; background-color: red">
   <a href="http://google.com">Your link</a>
</div>
$("div").click(function(){
   window.location=$(this).find("a").attr("href"); return false;
});
当你越过div时,

cursor:pointer得到正确的光标。 在ajax环境中,您可以通过以下方式获得更好的结果:

$.ajax({
  url: "test.html",
}).done(function() {
  // handle successfull operations
});

阅读documentation了解详情。