如何在 jQuery 选择器中使用 JavaScript 变量? [英] How to use JavaScript variables in jQuery selectors?

查看:36
本文介绍了如何在 jQuery 选择器中使用 JavaScript 变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 jQuery 选择器中使用 JavaScript 变量作为参数?

How do I use 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 的元素,该元素的 id 等于被点击的元素的名称.

Basically what I want to do is to be able to hide the element which has an id that is equal to the name of the element that is being clicked.

推荐答案

var name = this.name;
$("input[name=" + name + "]").hide();

或者你可以做这样的事情.

OR you can do something like this.

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();

你也可以在这个中使用它.

You can also use it in this also.

$("#" + this.id).slideUp('slow', function (){
    $("#" + this.id).remove();
});

这篇关于如何在 jQuery 选择器中使用 JavaScript 变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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