如何禁用html的a标签中的onclick事件

如何禁用html的a标签中的onclick事件,第1张

你指的是禁用默认事件

<a href="baidu.com">百度</a>

1.jQuery 阻止默认事件

$("a").click(function( event ) {

    event.preventDefault() // 阻止默认事件

    event.stopPropagation() // 阻止冒泡

})

2.javascript 

var elem = document.getElementByTagName("a")

elem.addEventListener("click",function(event){

    event.preventDefault() // 阻止默认事件

    event.stopPropagation() // 阻止冒泡

 },false)

说明:

preventDefault()   // 阻止默认事件

stopPropagation()  // 阻止冒泡

return false   // 既阻止默认事件 也阻止冒泡

/// <summary>

/// 去除所有HTML标记

/// </summary>

public static string DeleteHtmlTag(string html)

{

html = Regex.Replace(html, @"<script[^>]*?>[\s\S]*?</script>", "", RegexOptions.IgnoreCase)

html = Regex.Replace(html, @"<style[^>]*?>[\s\S]*?</style>", "", RegexOptions.IgnoreCase)

html = Regex.Replace(html, @"<(.[^>]*)>", "",RegexOptions.IgnoreCase)

html = Regex.Replace(html, @"([\r\n])[\s]+", "",RegexOptions.IgnoreCase)

html = Regex.Replace(html, @"<!--(.*?)-->", "", RegexOptions.IgnoreCase)

html = Regex.Replace(html, @"“", "“", RegexOptions.IgnoreCase)

html = Regex.Replace(html, @"”", "”", RegexOptions.IgnoreCase)

html = Regex.Replace(html, @"&(.*?)", "", RegexOptions.IgnoreCase)

html = html.Replace("<", "")

html = html.Replace(">", "")

html = html.Replace("\r\n", "")

return html

}

/// <summary>

/// 去除脚本,样式,框架,事件等标记

/// </summary>

public static string DeleteAnyHtmlTag(string html)

{

html = Regex.Replace(html, @"<select[^>]*?>[\s\S]*?</select>", "", RegexOptions.IgnoreCase)

html = Regex.Replace(html, @"<script[^>]*?>[\s\S]*?</script>", "", RegexOptions.IgnoreCase)

html = Regex.Replace(html, @"<style[^>]*?>[\s\S]*?</style>", "", RegexOptions.IgnoreCase)

html = Regex.Replace(html, @"<iframe[^>]*?>[\s\S]*?</iframe>", "", RegexOptions.IgnoreCase)

html = Regex.Replace(html, @"<link[^>]*?>", "", RegexOptions.IgnoreCase)

html = Regex.Replace(html, @"<input[^>]*?>", "", RegexOptions.IgnoreCase)

html = Regex.Replace(html, @" id.*?=.*?""[^>]*?""", "", RegexOptions.IgnoreCase)

html = Regex.Replace(html, @" class.*?=.*?""[^>]*?""", "", RegexOptions.IgnoreCase)

html = Regex.Replace(html, @" style.*?=.*?""[^>]*?""", "", RegexOptions.IgnoreCase)

html = Regex.Replace(html, @" on.*?=.*?""[^>]*?""", "", RegexOptions.IgnoreCase)

return html

}

在html中要使a标签不可用,需要在onclick时返回false即可,具体代码如下所示:

1、通过设置onclick属性使其不可用

<a href="http://mail.163.com" onclick="return false">HTML控制链接不可用</a>

2、通过js动态控制使其不可用

<a id="link" href="http://mail.163.com" onclick="return false">JS控制链接不可用</a>

<script>

document.getElementById("link").onclick=function(){return false}

</script>


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/6284175.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-19
下一篇 2023-03-19

发表评论

登录后才能评论

评论列表(0条)

保存