app.directive('a', function() {
return {
restrict: 'E', // only Elements (<a>),
link: function(scope, elm, attr) {
// every time you click on the link
elm.on('click', function($event) {
console.log('event')
$event.preventDefault()
return false
})
}
}
})
app.directive('a', function() {
return {
restrict: 'E', // only Elements (<a>),
link: function(scope, elm, attr) {
// every time you click on the link
elm.on('click', function($event) {
// only some URLs
if(attr.href.match(/youtube\.com/)) {
$event.preventDefault()
return false
}
})
}
}
})
document.addEventListener("DOMContentLoaded", () => {
document.querySelectorAll('a').forEach(a => {
a.addEventListener("click", (event) => {
event.preventDefault(); // Block the page from loading
alert(a.href); // Show an alert with the url in the href
})
})
});
3条答案
按热度按时间yxyvkwin1#
使用angular,您可以为元素
<a>
添加一个指令,并在click
上添加一个侦听器字符串
多田!
现在,如果你想阻止一些URL,你可以通过
attr.href
访问link函数中的href元素,所以你可以这样做:型
6jygbczu2#
这个解决方案使用vanilla JS:
个字符
6vl6ewon3#
我为此做了一个基于jQuery的示例。
个字符