jquery中获取id值方法小结
作者:mdxy-dxy 发布时间:2024-04-19 10:19:25
标签:jquery获取id
先判断是jquery对象还是html对象, 如果是jquery对象, 可以直接用 jquery对象.attr("id")获取
如果是html对象,可以用 html对象.id 或者 $(html对象).attr("id")
一般来说 $("#id").val()或者
下面脚本之家小编就整理一下常用几种获取方式
例如页面内容如下
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>jquery获取id值—asp之家</title><script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/1.11.3/jquery.min.js"></script></head><body><div id="asp"><a href="https://www.aspxhome.com">asp之家</a></div><div id="asptxt">脚本之家</div><div class="aspclass">脚本之家</div><input type="text" value="aspnet" class="okasp" /><script>var title=$('title').text(); console.log(title);//因为title是标签不用加 .或者 #var idasp=$('#asp').html();//id使用#var idasptxt=$('#asptxt').html();</script></body></html>
测试
$('#asp').html()//'<a href="https://www.aspxhome.com">asp之家</a>'$('#asp').text()//'asp之家'
如果只想获取文字就用
$('#asp').text()'asp之家'
如果想获取里面的html元素就用
$('#asp').html()'<a href="https://www.aspxhome.com">asp之家</a>'
id与classj就是.与#的区别,但一般来说class都是复用的,需要判断究竟要第几个。
如果是想获取页面中的制定id或者一些自定义属性值就需要用attr、prop、data等方式,具体的可以参考这篇文章
jQuery 获取与设置 元素属性
<div id="product"> </div><script language = "JavaScript" type="text/javascript">$(document).ready(function(){name = $('div').eq(0).attr('id');alert(name)});
eq(0)是取第一个jq元素。。。
eq(index)匹配一个给定索引值的元素
Matches a single element by its index.返回值Element
参数index (Number) : 从 0 开始计数
示例查找第二行
HTML 代码:
<table><tr><td>Header 1</td></tr><tr><td>Value 1</td></tr><tr><td>Value 2</td></tr></table>
jQuery 代码:
$("tr:eq(1)")
结果:
[ <tr><td>Value 1</td></tr> ]
获取不同id的值
<script src="js/jquery.js"></script><script type="text/javascript"><!--$(document).ready(function(){var len = $("#group span").size();//获取span标签的个数var arr = [];for(var index = 0; index < len-1; index++){//创建一个数字数组arr[index] = index;}$.each(arr, function(i){//循环得到不同的id的值var idValue = $("#group span").eq(i).attr("id");if(idValue != ''){alert(idValue);}});});//--></script><span id="group"><span id="0_1">aaa,<span group_id="0_1" class="icon_close"> </span></span><span id="0_2">bbb,<span group_id="0_2" class="icon_close"> </span></span><span id="0_3">ccc,<span group_id="0_3" class="icon_close"> </span></span><span id="0_4">ddd,<span group_id="0_4" class="icon_close"> </span></span><span id="0_5">eee,<span group_id="0_5" class="icon_close"> </span></span></span>
这样就会得到你想要的所有的id:
0_10_20_30_40_5
文本框,文本区域:
$("#txt").attr("value",'');//清空内容$("#txt").attr("value",'11');//填充内容
多选框checkbox:
$("#chk1").attr("checked",'');//不打勾$("#chk2").attr("checked",true);//打勾if($("#chk1").attr('checked')==undefined) //判断是否已经打勾
0
投稿
猜你喜欢
- 最近在做一个领券功能的时候,发现在一定并发下会出现重复领券的问题。使用度娘一顿搜索操作之后,发现可以使用分布式锁来解决这个问题。什么是分布式
- 一. 网页挂马的概念: 网页挂马是指:在获取网站或者网站服务器的部分或者全部权限后,在网
- 图像的轮廓检测,如计算多边形外界、形状毕竟、计算感兴趣区域等。Contours : Getting Started轮廓简单地解释为连接所有连
- 本文实例讲述了Python 面向对象之类class和对象基本用法。分享给大家供大家参考,具体如下:类(class):定义一件事物的抽象特点,
- 目录序列容器序列与扁平序列不可变序列与可变序列列表推导生成器表达式Tips小结序列序列是指一组数据,按存放类型分为容器序列与扁平序列,按能否
- 本文实例讲述了PHP+Mysql基于事务处理实现转账功能的方法。分享给大家供大家参考。具体如下:<?php header(
- 记录了mysql 8.0.21 的安装配置方法,分享给大家。一、下载1、下载安装包mysql下载路径直接点击链接也可以下载:mysql 8.
- python中,遍历dict的方法有四种。但这四种遍历的性能如何呢?我做了如下的测试l = [(x,x) for x in xrange(1
- 问题现象从阿里云上镜像过来的一台数据库服务器,SQL Agent服务启动不了,提示服务启动后停止。(原数据库服务器是正常的,怀疑跟镜像有关)
- 网上关于PyQt5的教程很少,特别是界面跳转这一块儿,自己研究了半天,下来和大家分享一下一、首先是主界面# -*- coding: utf-
- PHP getName() 函数实例返回 XML 元素及其子元素的名称:<?php $xml=<<<XML<?
- 前情提要:作为刚入门机器视觉的小伙伴,第一节课学到机器视觉语法时觉得很难理解,很多人家的经验,我发现都千篇一律,功能函数没解析,参数不讲解,
- 如何修改数据库名(db_name)及实例名(Instance_name or Service_name) Nid是Oracle从9iR2开始
- HTML代码: <div class="float" id="float"> 我是个腼腆
- cgi.h #ifndef CGI_H#define CGI_H#include <stdio.h>#include <s
- 本文实例讲述了Python打印斐波拉契数列的方法。分享给大家供大家参考。具体实现方法如下:#打印斐波拉契数列#!/usr/bin/pytho
- 一、安装前准备查看数据库版本命令: mysql --versionmysql-community-common-8.0.17-1.el7.x
- golang作为一热门的兼顾性能 效率的热门语言,相信很多人都知道,在编程语言排行榜上一直都是很亮眼,作为一门强类型语言,二进制位的操作肯定
- 基本索引In [4]: sentence = 'You are a nice girl'In [5]: L = senten
- 概述Mysql的Replication(复制)是一个异步的复制过程,从一个 Mysql instance(我们称之为 Master)复制到另