/#([^#]+)#/.test(content) ? content.replace(/#([^#]+)#/g, `<a href="javascript:void(0)" chref="快捷提问://$1" onclick="event.preventDefault();">$1</a>`),给其中替换的a标签绑定事件
时间: 2024-02-09 16:13:20 浏览: 22
可以使用事件委托的方式为替换的a标签绑定事件。具体方法如下:
```js
// 获取包含替换后的a标签的父元素
const parentElement = document.querySelector(".parent-element");
// 为父元素绑定点击事件,使用事件委托为替换后的a标签绑定点击事件
parentElement.addEventListener("click", function(event) {
// 获取被点击的元素
const clickedElement = event.target;
// 判断被点击的元素是否为替换后的a标签
if (clickedElement.tagName.toLowerCase() === "a" && clickedElement.getAttribute("chref")) {
// 阻止默认事件
event.preventDefault();
// 获取chref属性的值
const chrefValue = clickedElement.getAttribute("chref");
// 自定义逻辑
console.log(`用户点击了链接:${chrefValue}`);
}
});
```
在这个例子中,我们首先获取了包含替换后的a标签的父元素,然后为该父元素绑定了点击事件。当用户点击该父元素内的元素时,会触发该事件。我们通过判断被点击的元素是否为替换后的a标签,并且该a标签是否有chref属性,来确定用户是否点击了替换后的a标签。如果用户点击了替换后的a标签,我们就使用getAttribute方法获取该a标签的chref属性的值,并且进行自定义逻辑处理。在这个例子中,我们只是简单地输出了一条日志信息,你可以根据实际需求来编写自己的逻辑处理代码。
相关推荐
![js](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![md](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
import os from bs4 import BeautifulSoup import re # 指定文件夹路径 folder_path = "C:/Users/test/Desktop/DIDItest" # 正则表达式模式 pattern = r'<body>(.*?)<\/body>' # 遍历文件夹中的所有文件 for root, dirs, files in os.walk(folder_path): for file in files: # 读取html文件 file_path = os.path.join(root, file) with open(file_path, "r", encoding="utf-8") as f: html_code = f.read() # 使用正则表达式匹配<body>标签内的数据 body_data = re.findall(pattern, html_code, re.DOTALL) # 剔除和() body_data = body_data[0].replace("", "").replace("()", "") # 使用正则表达式提取talk_id、时间、发送者ID和接收者ID matches = re.findall(r'\[talkid:(\d+)\](\d+年\d+月\d+日 \d+:\d+:\d+).*?<span.*?>(\d+)<.*?>(.*?)<', body_data) # 提取唯一ID,时间,发送号码和私聊群聊关键词 matches1 = re.findall(r'<span.*?hint-success.*?>(\d+)<.*?>', body_data) # match = re.search('(中发言|发送)\s(.*?)\s', body_data) # if match: # content = match.group(2) matches2 = re.findall('(中发言|发送)\s(.*?)\s', body_data) for match in matches2: content = match[1] soup = BeautifulSoup(content, 'html.parser') if soup.find('= 2: receive_id = matches1[3] # 处理匹配结果 for match in matches: talk_id = match[0] time = match[1] send_id = match[2] talk_type = match[3] # 进行时间格式转换,将time转换为"0000-00-00"格式 time = time.replace('年', '-').replace('月', '-').replace('日', '') talk_type = talk_type.replace('向', '私聊').replace('在群', '群聊') # 打印结果 print("Talk ID:", talk_id) print("Time:", time) print("Sender ID:", send_id) print("Receive_id:", receive_id) print("Talk_type:", talk_type) print("Content:",content) print("---")导入至csv
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)