使用 jQuery 从标签中获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3020962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:44:00  来源:igfitidea点击:

Get values from label using jQuery

jquerylabel

提问by Pankaj

I want to get month and year value from label. How can i get these using jquery?

我想从标签中获取月份和年份值。我如何使用 jquery 获得这些?

<label year="2010" month="6" id="current Month"> June &nbsp;2010</label>

回答by Gavin Mogan

Firstly, I don't think spaces for an id is valid.

首先,我认为 id 的空格无效。

So i'd change the id to not include spaces.

所以我将 id 更改为不包含空格。

<label year="2010" month="6" id="currentMonth"> June &nbsp;2010</label>

then the jquery code is simple (keep in mind, its better to fetch the jquery object once and use over and over agian)

那么jquery代码很简单(请记住,最好一次获取jquery对象并一遍又一遍地使用)

var label = $('#currentMonth');
var month = label.attr('month');
var year = label.attr('year');
var text = label.text();

回答by icktoofay

You can use the attrmethod. For example, if you have a jQuery object called label, you could use this code:

您可以使用该attr方法。例如,如果您有一个名为 的 jQuery 对象label,则可以使用以下代码:

console.log(label.attr("year")); // logs the year
console.log(label.attr("month")); // logs the month

回答by nebkat

Use .attr

使用 .attr

$("current_month").attr("month")
$("current_month").attr("year")

And change the labels id to

并将标签 ID 更改为

<label year="2010" month="6" id="current_month"> June &nbsp;2010</label>

回答by Kevin Le - Khnle

I am changing your id to current-month(having no space)

我正在将您的 ID 更改为当前月份(没有空格)

alert($('#current-month').attr('month'));
alert($('#current-month').attr('year'));

回答by hari

var label = $('#current_month');
var month = label.val('month');
var year = label.val('year');
var text = label.text();
alert(text);

<label year="2010" month="6" id="current_month"> June &nbsp;2010</label>

回答by DON

Try this:

尝试这个:

var label = $('#currentMonth').text()

回答by David says reinstate Monica

While this question is rather old, and has been answered, I thought I'd take the time to offer a couple of options that are, as yet, not addressed in other answers.

虽然这个问题已经很老了,并且已经得到了回答,但我想我会花时间提供一些选项,这些选项目前尚未在其他答案中解决。

Given the corrected HTML (camelCasing the idattribute-value) of:

给定以下更正的 HTML(camelCasingid属性值):

<label year="2010" month="6" id="currentMonth"> June &nbsp;2010</label>

You could use regular expressions to extract the month-name, and year:

您可以使用正则表达式来提取月份名称和年份:

// gets the eleent with an id equal to 'currentMonth',
// retrieves its text-content,
// uses String.prototype.trim() to remove leading and trailing white-space:
var labelText = $('#currentMonth').text().trim(),
    // finds the sequence of one, or more, letters (a-z, inclusive)
    // at the start (^) of the string, and retrieves the first match from
    // the array returned by the match() method:
    month = labelText.match(/^[a-z]+/i)[0],
    // finds the sequence of numbers (\d) of length 2-4 ({2,4}) characters,
    // at the end ($) of the string:
    year = labelText.match(/\d{2,4}$/)[0];

var labelText = $('#currentMonth').text().trim(),
    month = labelText.match(/^[a-z]+/i)[0],
    year = labelText.match(/\d{2,4}$/)[0];

console.log(month, year);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label year="2010" month="6" id="currentMonth"> June &nbsp;2010</label>

Rather than regular expressions, though, you could instead use custom data-*attributes (which work in HTML 4.x, despite being invalid under the doctype, but are valid under HTML 5):

但是,除了正则表达式之外,您还可以使用自定义data-*属性(在 HTML 4.x 中工作,尽管在 doctype 下无效,但在 HTML 5 下有效):

var label = $('#currentMonth'),
    month = label.data('month'),
    year = label.data('year');

console.log(month, year);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label data-year="2010" data-month="6" id="currentMonth"> June &nbsp;2010</label>

Note that this will output 6(for the data-month), rather than 'June'as in the previous example, though if you use an array to tie numbers to month-names, that can be solved easily:

请注意,这将输出6(对于data-month),而不是'June'前一个示例中的输出,但如果您使用数组将数字与月份名称联系起来,则可以轻松解决:

var monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
    label = $('#currentMonth'),
    month = monthNames[+label.data('month') - 1],
    year = label.data('year');

console.log(month, year);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label data-year="2010" data-month="6" id="currentMonth"> June &nbsp;2010</label>

Similarly, the above could be easily transcribed to the native DOM (in compliant browsers):

类似地,上面的内容可以很容易地转录到原生 DOM(在兼容的浏览器中):

var monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
    label = document.getElementById('currentMonth'),
    month = monthNames[+label.dataset.month - 1],
    year = label.dataset.year;

console.log(month, year);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label data-year="2010" data-month="6" id="currentMonth"> June &nbsp;2010</label>

References:

参考: