简体   繁体   English

防止标签重定向到URL

[英]Prevent a tag to redirect on URL

I am stuck at very normal scnerio. 我被困在非常正常的环境中。 I have HTML code generated by YII CLINK PAGER Pagination widget : 我有由YII CLINK PAGER分页小部件生成的HTML代码:

<ul class="yiiPager" id="yw0">
      <li class="first hidden"><a href="/advisory_system/administrator/index.php/specialCategory/gridview?optget_grid=&amp;analysis_type=4&amp;_=1410520989427">&lt;&lt; First</a></li>
      <li class="previous hidden"><a href="/advisory_system/administrator/index.php/specialCategory/gridview?optget_grid=&amp;analysis_type=4&amp;_=1410520989427">&lt; Previous</a></li>
      <li class="page selected"><a href="/advisory_system/administrator/index.php/specialCategory/gridview?optget_grid=&amp;analysis_type=4&amp;_=1410520989427">1</a></li>
      <li class="page"><a href="/advisory_system/administrator/index.php/specialCategory/gridview?optget_grid=&amp;analysis_type=4&amp;_=1410520989427&amp;page=2">2</a></li>
 </ul>

and I want AJAX pagination for my requirement, so that I have wrote Jquery code : 并且我想要AJAX分页来满足我的要求,以便我编写了Jquery代码:

 $("ul.yiiPager li.page a").on('click',function (e){
        e.preventDefault();
        alert($(this).attr('href'));
        return false;
        loadlistData($(this).attr('href'));
    }); 

But by clicking on any of the <a> tag it is redirecting to the LINK given in href for preventing that i have used e.preventDefault(); 但是通过单击任何<a>标记, 它将重定向到href中给定的LINK,以防止我使用e.preventDefault(); but still it is not coming in JQuery code and alert not showing. 但它仍然没有出现在JQuery代码中,并且警报未显示。

Please help. 请帮忙。 Thanks in advance. 提前致谢。

Since your anchor tags are created dynamically. 由于您的定位标记是动态创建的。 You need to use event delegation. 您需要使用事件委托。 Because the elements should be present on the dom at the time of event binding. 因为元素应该在事件绑定时出现在dom上。 In the case of event delegation events are binded to the document or parent element which is presented on the dom 在事件委托的情况下,事件绑定到dom上显示的document或父元素

$(document).on('click', "ul.yiiPager li.page a", function(e) {
    e.preventDefault();
    alert($(this).attr('href'));
    loadlistData($(this).attr('href'));
});

Case 1 (direct): 情况1(直接):

$("ul.yiiPager li.page a").on("click", function() {...}); $(“ ul.yiiPager li.page a”)。on(“ click”,function(){...}); I want every ul.yiiPager li.page a inside ul.yiiPager li.page to listen up: when you get clicked on, do something. 我希望每个ul.yiiPager li.page里面的ul.yiiPager li.page都可以监听:单击时,请执行某些操作。

Case 2 (delegated): 案例2(授权):

$("ul.yiiPager li.page").on("click", "a", function() {...}); $(“ ul.yiiPager li.page”)。on(“ click”,“ a”,function(){...}); ul.yiiPager li.page When any of your child elements which are "a" get clicked, do something with them. ul.yiiPager li.page当您的任何子元素“ a”被点击时,请对其进行操作。

Summary 摘要

In case 1, each of those spans has been individually given instructions. 在情况1中,已分别给每个跨度指示。 If new spans get created, they won't have heard the instruction and won't respond to clicks. 如果创建了新的跨度,他们将不会听到说明,也不会响应点击。 Each a is directly responsible for its own events. 每个a直接负责自己的事件。

In case 2, only the container has been given the instruction; 在第2种情况下,仅向容器提供了指令; it is responsible for noticing clicks on behalf of its child elements. 它负责代表其子元素注意点击。 The work of catching events has been delegated. 捕获事件的工作已委派。

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

相关问题 使用 hash 标签时防止页面重定向 - Prevent page redirect when using hash tag 使用http图片标签src阻止重定向 - Prevent a redirect with http image tag src preventDefault javascript不会阻止url重定向 - preventDefault javascript doesnt prevent url redirect 使用变量和Google跟踪代码管理器重写和重定向网址 - Rewrite and redirect a url with variables and google tag manager 如何防止自动解析 URL 标记并将其呈现为完整标记? - How to prevent auto parsing of an URL tag and render it as the full tag instead? ngTouch 模块阻止从 href 属性重定向到 url - ngTouch module prevent redirect to url from href attribute 防止用户直接访问 url 并在未登录时重定向到登录 Flask - Prevent users from directly accessing url and redirect to login if not logged in Flask jsp重定向url标签和response.sendRedirect()方法的区别 - Difference between jsp redirect url tag and response.sendRedirect() method 单击锚标记时如何将 1 个网址重定向到另一个网址? - How to Redirect 1 url to another when you click on anchor tag? 如何检测URL并重定向到404(如果其中包含脚本标签) - How to detect URL and redirect to 404 if it contains script tag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM