在jQuery中获取html页面的id
Get the id of html page in jQuery?
我的页面上有一个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');
});
相关文章:
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- 一个正则表达式,用于从JS中的HTML标记中删除id、样式和类属性
- Html地图对象-点击地图获取id的一部分
- tu如何将id放在填充了json数据的html表td上
- 如何重写Javascript If语句以选择Classes而不是ID's的HTML格式
- ID为的HTML样式类
- jQuery值在html中作为id传递
- 获取 HTML 5 音频控制文件位置并在不使用 id 选择器的情况下更新 src
- 对javascript使用.html中的select id
- 如何筛选要写入新文档的HTML,删除JavaScript中的某些类和/或ID
- 我想使用Javascript中的循环在HTML中的特定id中显示数组中的多个对象
- 如何将value和id动态添加到html下拉选项标记中
- 如何使用 html 类或 id 在任何地方显示内容
- 在库(javascript/HTML)中将“name”替换为“id”
- 使用 PHP 从 HTML 表中获取 DATA-ID HTML
- 在数据响应中选择 #id.html
- 如何在特定InnerHTML上的类HTML元素上创建ID?HTML没有ID,使用JavaScript
- 我可以像这样发送html的值$("#id").html()给后面的代码吗?
- jquery $("#id").html(data)不能在firefox上工作,但可以在chrome
- 为什么我的 javascript 函数不交换编写的 pi id html 内容