jQuery等同于“getElementsByName”; [英] jQuery equivalent to "getElementsByName"

查看:112
本文介绍了jQuery等同于“getElementsByName”;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

getElementsByName 调用的正确jquery语法是什么?

What is the correct jquery syntax for a getElementsByName call?

这是我的javascript代码:

Here is my javascript code:

var test = document.getElementsByName(tableName)[0];

使用此值返回不同的值:

using this is returning a different value:

var test = $("[name=tableName]");

提前致谢

推荐答案

属性选择器周围使用引号:

$('[name="somenamehere"]');

如果需要在选择器中使用变量,则需要使用字符串连接来获取值变量:

If you need to use a variable within a selector, you need to use string concatenation to get the value of the variable:

$('[name="' + tableName + '"]');






通常应避免使用 [name] 属性支持 [id] 属性,因为选择会更简单:


Typically one should avoid using the [name] attribute in favor of the [id] attribute, because selection would be simpler as:

$('#someidhere');
-or-
$('#' + tableID);

这篇关于jQuery等同于“getElementsByName”;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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