简体   繁体   English

如何从原始HTML获取HTML div属性的值?

[英]How to get HTML div attribute's value from a raw HTML?

I have extracted this HTML using find(). 我已经使用find()提取了此HTML。

<div class="merged_doc_list_sub_divs" div_type="pdf_doc" id="1500439">
Warwick University response
<img onclick="remove_doc_div(1500439)" src="/img/close.png" style="width:3%;float:right;cursor:initial;">
</div>

Now I want to extract id from this DIV and i tried using .attr('id') but it's giving me an error saying attr() is not a valid function, because the HTML is raw html stored in a variable. 现在,我想从该DIV中提取ID,我尝试使用.attr('id'),但这给我一个错误,说attr()不是有效函数,因为HTML是存储在变量中的原始html。

This is what I tried 这是我尝试过的

var destination_list = $('#merged_document_div').find('div.merged_doc_list_sub_divs');
if (destination_list.length > 0)
{
    for (index in destination_list)
    {
        if (rowData['doc_id'] == destination_list.attr('id'))
        {
            rowData["row_exist"] = true;
        }
    }
}

So please let me know how do I do this? 所以,请让我知道我该怎么做?

Directly use selector to check whether element exists 直接使用选择器检查元素是否存在

var destination_list = $('#merged_document_div').find('div.merged_doc_list_sub_divs#' + rowData['doc_id']);
rowData["row_exist"] = !!destination_list.length;

However as its ID which will be unique just directly use ID selector 但是作为唯一的ID,只需直接使用ID选择器

rowData["row_exist"] = !!$('#' + rowData['doc_id']).length;

If you are using ID to store some arbitrary data, then I would recommend you to use data-* prefixed custom attributes 如果您使用ID来存储一些任意数据,那么我建议您使用带data-*前缀的自定义属性

HI Keyur, HI Keyur,

Instead of using for loop, use each loop - 代替使用for循环,使用每个循环-

$.each(destination_list, function () {
                if (this.id == rowData['doc_id']) {
                    alert("div found");
                }
            });

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

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