我可以将(单击)事件的函数添加到我使用替换方法添加的跨度标签中吗?

发布于 2025-01-28 12:30:58 字数 430 浏览 4 评论 0原文

https://i.sstatic.net/b5dow.png 我有这样的句子,如您所见,如果我要寻找的单词在该句子中,我将其放在跨度标签中。

.replace(query.toLocaleLowerCase(), `<span>${query}</span>`,).toLocaleLowerCase()

(查询是我的搜索字) 我想在这样的跨度标签中拿起这个词,

`<span (click)="myFunction()">${query}</span>`

有办法做到这一点吗?

https://i.sstatic.net/B5doW.png
I have sentences like this, and as you can see, if the word I'm looking for is in that sentence, I put it in span tags.

.replace(query.toLocaleLowerCase(), `<span>${query}</span>`,).toLocaleLowerCase()

(query is my search word)
and I want to make the word taken in these span tags like this

`<span (click)="myFunction()">${query}</span>`

Is there a way to do this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ペ泪落弦音 2025-02-04 12:30:59

我得到了这样的解决方案,这不是很正确,但是最简单的

ts side

click(evt) {
const href = evt.target.localName;

if (href == 'span') {
  evt.path.forEach(element => {
    if (element.localName == "p") {
       ---my codes---
    }
  });
}

}

html侧;

(click)="click($event)" [innerHtml]="subtitle"

I got the solution like this, it's not quite right, but the simplest

TS Side

click(evt) {
const href = evt.target.localName;

if (href == 'span') {
  evt.path.forEach(element => {
    if (element.localName == "p") {
       ---my codes---
    }
  });
}

}

HTML side;

(click)="click($event)" [innerHtml]="subtitle"
只是在用心讲痛 2025-02-04 12:30:58

你尝试过吗?

.replace(query.toLocaleLowerCase(), `<span onclick="functionName()">${query}</span>`,).toLocaleLowerCase()

Have you tried

.replace(query.toLocaleLowerCase(), `<span onclick="functionName()">${query}</span>`,).toLocaleLowerCase()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文