在jQuery中获取html页面的id

Get the id of html page in jQuery?

本文关键字:id html jQuery 获取      更新时间:2023-09-26

我的页面上有一个id,我通过在变量中使用.html()函数获取了所有html。现在,我想使用存储html的变量中的id。如何在jQuery中做到这一点?我不知道。请帮帮我!

var idhtml= $("#id").html();

像这样,我在idhtml 中有html

<input type="text" id="id1" name="id1" value="" class ="test" />
<input type="text" id="id2" name="id1" value="" class ="test" />
<input type="text" id="id3" name="id1" value="" class ="test" />

通过idhtml,我想得到变量中的id。

您可以获得如下ID:

$("input[id^='id']").each(function () { 
    console.log(this.id); 
    // Change the id:
    $(this).attr("id", "new_id");
});

选择id以id开头的每个input,对于返回的每个输入,在控制台中打印其id。

您可以使用.each()

要设置属性,可以使用

1) .attr('attribute_name','attribute_value')

2) .data('attribute_name','attribute_value')

带有适当的选择器。

 $("[name='id1']").each(function () { 
     console.log($(this).attr(id));
     //to set the attr you can use
     $(this).attr('attribute_name','attribute_value');
     //to set custom attribute, use .data()
     $(this).data('attribute_name','attribute_value');
 });

尝试这个

  $(document).ready(function(){
        var formId = $('form').attr('youId');
    });