从jquery中的多个元素中进行选择
selecting from multiple elements in jquery
我正试图将此JavaScript代码更改为jQuery,但我不确定将onClick = 'function(this)'
与jQuery的.click
函数等同于什么。
JavaScript:
function setInput(text) {
var getClicked = text.name;
var click = document.getElementById(getClicked);
var show = document.getElementById('show_' + getClicked);
show.value = click.value;
}
HTML:
<input type='text' name = "a" id = "a" onclick='setInput(this);'>
<input type = "text" id = "show_a"><br>
<input type='text' name = "b" id = "b" onclick='setInput(this);'>
<input type = "text" id = "show_b">
$('input[type="text"]').on('click', function(){
$('#show_' + $(this).attr('name')).val($(this).val());
});
HTML:
<input class="clickme" type="text" name ="a" id ="a">
<input type ="text" id ="show_a"><br>
<input class="clickme" type="text" name ="b" id ="b">
<input type ="text" id ="show_b">
jQuery:
$(document).ready(function(){
$(".clickme").click(function(){
id = "#show_";
id += $(this).prop("id");
$(id).show();
});
});
Fiddle:http://jsfiddle.net/gTtHT/1/
$(".settable").click(function(e){
var getClicked = $(this).attr('name');
...
});
其中$(this)指的是已单击的元素。
如果DOM中已经存在输入,则以下操作将起作用。
$('#a, #b').click(function () {
setInput(this);
});
否则,请尝试
$(function() {
$('#a, #b').click(function () {
setInput(this);
});
});
相关文章:
- 使用Underscore.js修改json数组中所选元素的更有效方法
- 在所选元素的父元素上添加“ng-class”
- 查找所选元素的索引
- 如何覆盖jQuery所选元素的“click”事件
- 如何保持所选元素's节点的值(而不是引用)
- 在Telerik MVC树视图中显示所选元素
- 选择“;所选元素“;再次从选择框角度
- 如何将所选元素传递给'这'在链接函数中
- 获取所选元素的位置,而不是 DOM 中元素的索引
- 多选元素和样式选择选项
- 滚动条获胜't在所选元素不可见时滚动
- 从所选元素 html 上方的元素获取属性值
- 检查所选元素是否包含属性
- 单击角度表元素上的事件,将其定向到由所选元素的 ID 组成的第二个 URL
- 遍历所选元素及其子元素的所有类和 ID
- onChange 以及所选元素的值在哪里 (jQuery Mobile)
- 如何使用 jQuery 获取表的所有行中元素的 id
- Greasemonkey 脚本使用 jQuery 忽略附加到表行的元素
- VAR 在所选元素之间进行计数和切换,但会复制属性
- 从选中复选框的父行获取元素