[英]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=&analysis_type=4&_=1410520989427"><< First</a></li>
<li class="previous hidden"><a href="/advisory_system/administrator/index.php/specialCategory/gridview?optget_grid=&analysis_type=4&_=1410520989427">< Previous</a></li>
<li class="page selected"><a href="/advisory_system/administrator/index.php/specialCategory/gridview?optget_grid=&analysis_type=4&_=1410520989427">1</a></li>
<li class="page"><a href="/advisory_system/administrator/index.php/specialCategory/gridview?optget_grid=&analysis_type=4&_=1410520989427&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.