1

我正在尝试同时获取多个 Elements ID 的“数量”属性。

这是我尝试过的:

productMinimum : document.getElementsById("id1","id2").getAttribute("quantity")

我怎样才能使这项工作?“id1”和“id2”元素都有“数量”属性。

这是从 HTML 端看的样子:

<li class="product" name="RD-101" id="id1" price="18" quantity="2">
<li class="product" name="RD-101" id="id2" price="18" quantity="4"> 
4

7 回答 7

3

The problem you're having is that getElementsById() doesn't exist (unless you've defined it elsewhere). What you should be using is getElementById(), albeit twice (as getElementById(), as its name implies, returns only one element, even if there are multiple elements with that same id, which is invalid mark-up, so please don't do that), and then pushing the returned elements into an array:

var products = [];
products.push(document.getElementById("id1"));
products.push(document.getElementById("id2"));

You could, of course, create your own function to return multiple elements based on their id:

function getElementsById(ids) {
    if (!ids) {
        return false;
    }
    else {
        var elems = [];
        for (var i = 0, len = ids.length; i < len; i++) {
            if (document.getElementById(ids[i])) {
                elems.push(document.getElementById(ids[i]));
            }
        }
        return elems;
    }
}

console.log(getElementsById(['id1','id3']));​

JS Fiddle demo.

Bear in mind, though, that this returns a regular array, not a nodeList (as would be returned, for example, by getElementsByClassName()). And the returned array, even if it's only a single element, would have to be iterated over in the same way as any other array.

References:

于 2012-05-16T15:34:06.300 回答
1
function getQuantity(id) {
 return document.getElementById(id).getAttribute("quantity");
}

var quantId1 = getQuantity('id1');
var quantId2 = getQuantity('id2');

getElement* s *ById 正在返回一个数组。您需要获取各个项目。如果您有很多元素,您可以按类产品选择并编写一个简单的函数来循环它们并创建一个数组。

于 2012-05-16T15:37:16.790 回答
0

You can only get one element at once (with getElementById()). If you want an array containing the quantities, use this:

[document.getElementById('id1').getAttribute('quantity'), document.getElementById('id2').getAttribute('quantity')]

Consider using jQuery, there you can use $('#id1, #id2') and besides that it supports easy access to data- attributes - what you are doing right now is invalid HTML since li does not have price or quantity attributes:

<li class="product" name="RD-101" id="id1" data-price="18" data-quantity="2">
<li class="product" name="RD-101" id="id2" data-price="18" data-quantity="4"> 

To get the quantities array:

$('#id1, #id2').map(function() { return $(this).data('quantity'); }).get();
于 2012-05-16T15:33:40.627 回答
0

There is no such function as getElementsById. you can use either getElementsByClassName or getElementsByName

https://developer.mozilla.org/en/DOM/document.getElementsByName https://developer.mozilla.org/en/DOM/document.getElementsByClassName

Take note that getElementsByClassName is fairly new and not supported by older browsers.

于 2012-05-16T15:35:02.430 回答
0

使用纯 JavaScript,您需要为此编写自己的函数:

function getQuantities() {
    var args = Array.prototype.slice.call(arguments);
    var quantityValue = 0;
    for(var i = 0; i < args.length; i++) {
        quantityValue += document.getElementsById(args[i]).getAttribute("quantity");
    }
    return quantityValue;
}

// your code
productMinimum : getQuantities("id1","id2")
于 2012-05-16T15:35:21.777 回答
0

据我了解document.getElementById当时需要一个身份证

另外,考虑使用 html5 自定义数据属性

于 2012-05-16T15:35:31.733 回答
0

没有这样的事情getElementsById()

但是有querySelectorAll,但是IE7 和更早版本不支持它

这是一个示例,它应该在节点列表中返回这两个<li>

document.querySelectorAll('#id1, #id2')
于 2012-05-16T15:38:38.673 回答