简体   繁体   English

使用 javascript 从 html 字符串中读取 data-id 属性

[英]Read data-id attribute from html string using javascript

I am using Quill editor with user mentioning feature .我正在使用带有用户提及功能的 Quill 编辑器。 After the integration, editor generating the below result for the given text (Actual text).集成后,编辑器为给定文本(实际文本)生成以下结果。

Actual Text - this is from @Siddharth Choubey to @dev 3 Developer 3实际文本 - this is from @Siddharth Choubey to @dev 3 Developer 3

Below is the generated code by editor for the actual text.下面是编辑器为实际文本生成的代码。

<p>this is from 
    <span class="mention" data-index="0" data-denotation-char="@" data-id="114" data-value="Siddharth Choubey">
        <span contenteditable="false">
            <span class="ql-mention-denotation-char">@</span>
            Siddharth Choubey
        </span>
    </span>
     to 
     <span class="mention" data-index="2" data-denotation-char="@" data-id="132" data-value="dev 3 Developer 3">
        <span contenteditable="false">
            <span class="ql-mention-denotation-char">@</span>
            dev 3 Developer 3
        </span>
    </span> 
</p>

Now i am trying to read the each data-id attribute form the generated code separately & storing them in array using javascript .现在我正在尝试从生成的代码中分别读取每个data-id属性并使用 javascript 将它们存储在数组中。 I have tried the solution mentioned here But it is not working in my case.我已经尝试过这里提到的解决方案,但它不适用于我的情况。

I am unable to parse this generated code.我无法解析这个生成的代码。 Any one please help me on how can i read all data-id using javascript.任何人请帮助我如何使用javascript读取所有数据ID。

My code我的代码

 sendComment(){
        console.log(this.comment);
        const commentText=document.querySelectorAll('span.mention');
        commentText.forEach(btn => console.log(btn.getAttribute('data-id')))
    }

I found the issue in my code.我在我的代码中发现了这个问题。

sendComment(){
    let userIDList=[];
    console.log(this.comment);
    const commentText=document.querySelectorAll('span.mention');
    commentText.forEach(function(btn,index)
    {
        userIDList[index] = btn.getAttribute('data-id');
    });

    console.log(userIDList);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM