如何在jquery选择器中使用javascript变量 [英] How to use javascript variables in jquery selectors
本文介绍了如何在jquery选择器中使用javascript变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将javascript变量用作jquery选择器中的参数?
How do I use the javascript variables as a parameter in a jquery selector?
<script type="text/javascript">
$(function(){
$("input").click(function(){
var x = $(this).attr("name");
$("input[id=x]").hide();
});
});
</script>
<input type="text" id="bx"/><input type="button" name="bx"/>
<input type="text" id="by"/><input type="button" name="by"/>
基本上我想做的是能够隐藏id等于的元素要点击的元素的名称。
Basically what I want to do is to be able to hide the element which has id that is equal to the name of the element that is being clicked.
推荐答案
var name = this.name;
$("input[name=" + name + "]").hide();
或者你可以这样做。
var id = this.id;
$('#' + id).hide();
或者你也可以给出一些效果。
OR you can give some effect also.
$("#" + this.id).slideUp();
如果你想从页面中永久删除整个元素。
If you want to remove the entire element permanently form the page.
$("#" + this.id).remove();
您也可以在此使用它。
$("#" + this.id).slideUp('slow', function (){
$("#" + this.id).remove();
});
这篇关于如何在jquery选择器中使用javascript变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文