How to add click event to dynamically added html element in typescript

前端 未结 3 1311
天涯浪人
天涯浪人 2020-12-01 21:18

I\'m building an app in angular 2. I want to add a click event to a dynamically added html element. I define a string (contentString), and in this string I define the html e

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-01 22:02

    In my case I do these--

    var boxText = document.createElement("div");
    const contentString = '' + this.mName + '
    ' + this.mObject.category + '
    Click here for more information ';//as per your code boxText.innerHTML = contentString; const button = document.createElement('button'); button.addEventListener('click', (e) => { this.navigate();//your typescript function }); button.innerText = 'Navigate here'; boxText.appendChild(button);

提交回复
热议问题