我想禁用div内的所有链接,并将其替换为那里的内容。jQuery:禁用div内的所有链接 - 替换内部内容
即
<div><a href="/blah.html">My super link</a></div>
到
<div>My super link</div>
感谢
我想禁用div内的所有链接,并将其替换为那里的内容。jQuery:禁用div内的所有链接 - 替换内部内容
即
<div><a href="/blah.html">My super link</a></div>
到
<div>My super link</div>
感谢
可以使用.replacewith()
和功能,这样做:
$("div a").replaceWith(function() { return $(this).text(); });
嗯 - 我的是类似于其他快速打字员的答案,但我还以为html()
本来你想用什么:
$('div a').each(function() {
$(this).replaceWith($(this).html());
});
+1 - 虽然我们的工作为例,这将适用于所有适用的情况。 – 2010-05-20 15:34:52
我知道这个问题是老了,但我这样做完成了这个以下:
1 - 选择,我想禁用所有锚
2 - 删除与锚
3相关的所有活动 - 用#替换href属性值
4 - 将点击事件添加到不执行任何操作的锚点
下面的代码示例从我的插件中删除。该插件也有代码 再次启用锚点。它可能会给你想法。
你也可以在(http://www.dougestep.com/dme/jquery-disabler-widget)下载我的插件并使用它或从中删除你需要的东西。
$('a').each(function(e) {
// disable events on anchor
this._disableEvents($(this));
// save off the HREF value
var href = inp.attr("href");
if (href != undefined) {
// save the HREF attribute value and remove the value
inp.data(dataAnchorHref, href);
inp.attr("href", "#");
}
// override the click event for the anchor
inp.on("click", function(e) {
e.preventDefault();
});
// decorate the anchor with a disabled look
inp.addClass('ui-state-disabled');
});
_disableEvents : function(inp) {
var de = $.Event("disableEvents");
this._trigger("disableEvents", de, inp);
if (de.isDefaultPrevented()) { return; }
var widgetEventPrefix = this.widgetEventPrefix;
// jQuery adds an "events" data attribute on the element when events are registered
var events = inp.data("events");
if (events != undefined) {
var savedEvents = [];
// loop through each event found on the element...
$.each(events, function(eventName, handlers) {
$.each(handlers, function(index) {
var handler = handlers[index];
if (handler != undefined) {
// save the event and handler
var eventObj = {
'eventName' : eventName,
'handler' : handler
};
if (eventName.indexOf(widgetEventPrefix) < 0) {
// unbinding a non widget event
savedEvents.push(eventObj);
inp.unbind(eventName);
}
} });
});
// store the saved events as a data attribute on the element
inp.data(dataSavedEvents, savedEvents);
}
}
+1与链接一起给一个解决方案的概要完整的软件包:-) – kleopatra 2012-11-17 17:50:48
这是否适用于图像和其他内容? – Chris 2010-05-20 15:49:50
@Chris - 在这种情况下使用'.contents()'而不是'.text()',在这里看到一个演示:http://jsfiddle.net/qd3E2/2/ – 2010-05-20 15:54:21