如果 ASP.NET 中的 href 为空,则禁用锚标记
我有一个 ASP.NET 菜单,它在渲染时生成许多锚标记。
我的要求是
- 防止回发如果 href 或 锚标记是“”或“#”。
- 使光标不显示 首先
,我检查了生成的锚标记之一的标记,
<a href="#"
class="popout level1 static"
tabindex="-1"
onclick="__doPostBack('ctl00$NavigationMenu','Unternehmen')">
Company
</a>
我看到了一个已经绑定的点击事件,并编写了一个快速的 jquery 片段。
$(document).ready(function () {
$(".menu a").each(function () {
var anchor = $(this);
var url = (anchor.attr('href').length == 0) ? "" : anchor.attr('href').trim();
if (url == "" || url == "#") {
//unbind the __dopostback
anchor.unbind('click');
anchor.bind('click',function (e) {
e.preventDefault();
});
anchor.css("cursor", "default");
}
});
});
当我将鼠标悬停在空链接上时,光标显示默认链接而不是手形,这意味着我的锚点已被识别。但是当我点击锚点时,发生了回发!
尝试将 anchor.unbind('click');
替换为 anchor.kill('click');
尝试通过附加 e.stopPropogation
替换 e.preventDefault();
,甚至 return false;
尝试用 anchor.click(function(e) {
替换 anchor.bind('click', function(e){
)
似乎没有效果。我的可能有什么问题代码?
I have an ASP.NET Menu which generates many anchor tags when rendered.
My requirements were
- to prevent postback if href or an
anchor tag is "" or "#". - to make the cursor not to show the
hand
First I checked the markup of one of the generated anchor tags
<a href="#"
class="popout level1 static"
tabindex="-1"
onclick="__doPostBack('ctl00$NavigationMenu','Unternehmen')">
Company
</a>
I saw an already bound click event and wrote a quick jquery snippet.
$(document).ready(function () {
$(".menu a").each(function () {
var anchor = $(this);
var url = (anchor.attr('href').length == 0) ? "" : anchor.attr('href').trim();
if (url == "" || url == "#") {
//unbind the __dopostback
anchor.unbind('click');
anchor.bind('click',function (e) {
e.preventDefault();
});
anchor.css("cursor", "default");
}
});
});
When I hovered on the empty link, the cursor is showing the default one instead of hand, that means my anchor is recognized. But when I clicked the anchor, postback occurred!
Tried replacing anchor.unbind('click');
with anchor.kill('click');
Tried replacing e.preventDefault();
by appending e.stopPropogation
and even return false;
Tried replacing anchor.bind('click', function(e){
with anchor.click(function(e) {
Nothing seems to work. What could be wrong with my code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试在代码中使用
anchor.removeAttr("onclick");
try using
anchor.removeAttr("onclick");
in your code为什么不使用这行简单的代码而不是删除事件处理程序呢?
实际上我想在页面查看模式下禁用我的链接。
这对我有用。
在代码隐藏中:
在源代码中:
Instead of removing eventhandler why not use this simple lines of code.
Actually I want to disable my link in view mode of page.
It worked for me.
In Code-behind:
In source-code:
与其删除事件处理程序,为什么不显式地将其替换为以下内容:
Instead of removing the event handler, why not explicitly replace it with something along the lines of:
如何使用空 href 或仅使用“#”href 循环遍历锚点。
编辑:
似乎
.unbind
只会取消绑定使用.bind
方法绑定的事件:/.removeAttr
是是:)How about looping through the anchors with an empty href or "#" href only.
EDIT:
Seems
.unbind
will only unbind events that you have bound using the.bind
method :/.removeAttr
is is :)