简体   繁体   English

如何同时调用onclick和href

[英]How to call onclick and a href in the same time

  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>";

I want that showDialogBox function be be executed when i click the div and in the same time the a href to work. 我希望在单击div并同时使用a href时执行showDialogBox函数。 In the code above the onclick does not work, only a href works. 在上面的代码中,onclick无效,只有href有效。 I want an ajax request to occur (and presumably actually succeed) and then for the browser to go ahead and locate to the link specified in the anchor tag 我希望发生一个ajax请求(大概实际上成功了),然后让浏览器继续前进并定位到anchor标签中指定的链接

In onclick function lets say clickme, on successful completion of ajax request write down the location of the page you wanted to redirect. 在onclick函数中,可以说clickme,在成功完成ajax请求后,记下要重定向的页面的位置。

for eg. 例如

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

call a onclick from div and redirect to the particular page from function using location.href 从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>";

in showDialogBox use location.href 在showDialogBox中使用location.href

For my knowledge you can just do the following. 就我所知,您可以执行以下操作。

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

Or am i not correctly understanding your question? 还是我不正确理解您的问题?

This will work for you: 这将为您工作:

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: HTML:

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

With data-href instead of href you don't need to prevent default event on your link. 使用data-href代替href您无需阻止链接上的默认事件。 If you want to use href make sure you e.preventDefault on this link. 如果要使用href确保在此链接上使用e.preventDefault

Java Script : Java脚本:

function func_search()
                {

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

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

                }

HTML : HTML:

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

Check Out this!! 看看这个! [http://jsfiddle.net/Y4qsp/] [http://jsfiddle.net/Y4qsp/]

It may help you ! 它可以帮助您! Give an id to your a tag , call onClick function on that id and call your function from the click function. 给您的ID提供一个标签,在该ID上调用onClick函数,然后从click函数调用您的函数。

The simplest thing that you ca do is something like (in jQuery): 您要做的最简单的事情是(在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;
});

cursor:pointer to get the right cursor when you are over the div. cursor:pointer ,当您在div上时可以获取正确的光标。 In ajax environment you could get better result with: 在ajax环境中,您可以获得以下更好的结果:

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

read the documentation for details. 阅读文档以获取详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM