获取相同的标记名称JavaScript [英] Get Same Tag Name JavaScript

查看:62
本文介绍了获取相同的标记名称JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi


Hi

<table>
    <tbody>
    <tr>
        <td>First name</td><td><input name="v[]" type="text" class="spec1" value="">
       </td>
    </tr>

    <tr>
        <td>Family name</td><td><input name="v[]" type="text" class="spec1" value="">
        </td>
    </tr>





代码我有2个输入标签同名〜> v []

如何获得元素第二标记?!

如果我通过此代码获得Elemnt:





in up code i have 2 input tags by same name ~> "v[]"
How Can to get Element Second Tag?!
if i get Elemnt By This Code :

document.getElementsByTagName("input")["v[]"].setAttribute('value', 'text');





那么第一次输入的工作标签

-------------------------------

怎样才能getElemntByValue?



then that work for first input Tag
-------------------------------
also how can getElemntByValue?

推荐答案

您可以使用 document.getElementsByName 来获取具有特定名称的所有元素。这将返回一个数组,您可以在其上进行迭代:

You can use document.getElementsByName to get all elements with a specific name. This returns an array, over which you can iterate:
var elems = document.getElementsByName("v[]");
for (var i = 0; i < elems.length; i++) {
    elems[i].setAttribute('value', 'text');
}







Quote:

还有getElemntByValue怎么样?

also how can getElemntByValue?



此方法不存在。你必须自己编写。

http://stackoverflow.com/a/16527048 [ ^ ]


我不知道看看它是如何有意义的。请查看我对该问题的评论。



在所有情况下,您都可以轻松浏览DOM并按任何功能或其组合查找任何元素,或者按顺序查找外观,如果你使用jQuery:

http://api.jquery.com/category/selectors

http://api.jquery.com/category/traversing ,< br $> b $ b https://api.jquery.com/each

< a href =https://api.jquery.com/jquery.each> https://api.jquery.com/jquery.each (令人困惑的是,同名,不同的每个)。



如果您需要学习jQuery(强烈推荐),请参阅:

< a href =http://en.wikipedia.org/wiki/JQuery> http://en.wikipedia.org/wiki/JQuery ,

http://jquery.com

http:/ /learn.jquery.com

http://learn.jquery.com/using-jquery-core

http://learn.jquery.com/about -jquery / how-jquery-works (从这里开始)。



如果你不想使用jQuery,你可以下载它的源代码看看你感兴趣的事情是如何实现的。 :-)



-SA
I don't see how it could make any sense. Please see my comment to the question.

In all cases, you can easily navigate through DOM and find any elements by any features or their combination, or also by order of appearance, if you use jQuery:
http://api.jquery.com/category/selectors,
http://api.jquery.com/category/traversing,
https://api.jquery.com/each,
https://api.jquery.com/jquery.each (confusingly, under the same name, different each).

If you need to learn jQuery (highly recommended), please see:
http://en.wikipedia.org/wiki/JQuery,
http://jquery.com,
http://learn.jquery.com,
http://learn.jquery.com/using-jquery-core,
http://learn.jquery.com/about-jquery/how-jquery-works (start from here).

If you don't want to use jQuery, you can download its source code and see how things you are interested in are achieved. :-)

—SA


这篇关于获取相同的标记名称JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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