Javascript 如何在html中设置函数的id

Javascript 如何在html中设置函数的id,javascript,html,Javascript,Html,我有HTML <a class="nav-link" id="sell" (click)="renderView(id)" <a class="nav-link" id="event" (click)="renderView(id)" 如何将值od id传递给函数?id未定义此处您可以使用onClick,例如: <a class="nav-link" id="sell" onclick="renderView(this.id)"> 希望这有帮助您提到的代码将

我有HTML

 <a class="nav-link" id="sell" (click)="renderView(id)" 

 <a class="nav-link" id="event" (click)="renderView(id)" 

如何将值od id传递给函数?id未定义

此处您可以使用onClick,例如:

<a class="nav-link" id="sell" onclick="renderView(this.id)">

希望这有帮助

您提到的代码将无法正常工作

将其改写为:

HTML:

首先,将(单击)更改为onclick,因为(单击)是无效的HTML。还可以添加文本和结束标记

其次,在此.id上执行此功能:

<a class="nav-link" id="sell" onclick="renderId(this.id)">Sell</a>
出售

(单击)=“renderView(id)
无效HTML它的可能副本应该是onclick,而不是(单击)未捕获引用错误:未在HtmlanchoreElement上定义renderView。onclick@ArekSzumacher如何在中添加函数?如何包含脚本?如果正确包含JS,则不会导致错误…@ArekSzumacher是否正确?我希望您已在同一HTML页面中添加JS函数renderView,或者n HTML页面中引用的JS文件。未捕获引用错误:renderView未在HtmlanchoreElement.onclick中定义,但将this.id传输到function请检查更新的答案,查看是否出现相同的错误,并使用function关键字。@ArekSzumacher
function renderView(id) {
   switch (id) {
     case 'detailInfo':
      console.log('detailInfo');
      break;
    case 'sell':
      console.log('sell');
      break;
    case 'event':
      console.log('event');
      break;
  }
}
<a class="nav-link" id="sell" onclick="renderView(this.id)" 
<a class="nav-link" id="event" onclick="renderView(this.id)" 
renderView(id) {
    switch (id) {
      case 'detailInfo':
        console.log('detailInfo');
        break;
      case 'sell':
        console.log('sell');
        break;
      case 'event':
        console.log('event');
        break;
    }
  }
<a class="nav-link" id="sell" onclick="renderId(this.id)">Sell</a>