如何在jQuery中获取容器的属性? [英] How to get attributes of container in jquery?

查看:168
本文介绍了如何在jQuery中获取容器的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用jquery从容器中获取属性值?

How can I get attributes values from an container using jquery ?

例如:

我的容器div为:

<div id = "zone-2fPromotion-2f" class = "promotion">

在这里,我如何使用jquery获取属性id值,然后如何修剪该值以获取组件信息?

here how can I get attribute id value using jquery and than how can I trim the value to get component information ?

更新:如何获取属性值?

update : how can i get attribute values ?

更新:如果页面上有多个具有相同div信息的组件,那么我将不知道该组件的属性值是什么?

UPDATE: If I have multiple components on page with same div information than how would I know what attribute value is for which component ?

谢谢.

推荐答案

首先,这似乎是一个荒谬的长ID,我相信它可以做得更短,同时仍保留其唯一性.

First, that seems to be a ridiculously long ID -- I'm sure it could be made much shorter while still retaining its uniqueness.

无论如何,继续回答:首先,您需要一种访问容器" div的方法.通常,可能会使用类或ID来获取元素.例如,您可以通过以下对jQuery的调用来选择"该div:

Anyway, on to the answer: First you need a way of accessing your "container" div. Typically, one might use a class or ID to get an element. For example, you could "select" this div with the following call to jQuery:

var container = jQuery('#zone-3a...'); // Fill in ... with really long ID

但是,由于您要询问如何检索ID,因此我认为不能通过ID选择它.您也可以使用该类来选择它,尽管不能保证它是该类页面上的唯一元素:

But, since you're asking how to retrieve the ID, I'm presuming that selecting it via the ID is not an option. You could also select it using the class, although it's not guarenteed to be the only element on the page with that class:

var container = jQuery('.promotion');

还有其他缩小搜索范围的方法,例如:

There are other ways to narrow down the search, such as:

jQuery('div.promotion');
jQuery('div.promotion:first');

一旦您引用了容器",就可以像这样检索ID:

Once you have a reference to your "container", you can retrieve the ID like so:

container.attr('id'); // => zone-3a...
// or:
container[0].id; // => zone-3a...

这篇关于如何在jQuery中获取容器的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆