有什么方法可以使用JS变量代替jquery选择器"#id"作为输入? [英] Is there any way to use a JS variable as input instead of jquery selector "#id"?

查看:347
本文介绍了有什么方法可以使用JS变量代替jquery选择器"#id"作为输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,我们能否在选择器内提供变量而不是#anyId.

I want to know that, can we able to give a variable instead of #anyId inside a selector.

实际上我正在尝试这段代码,

Actually i am trying this code,

<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<div id="aDiv123">
  <p id="abcd1234" class="abcd123">Click me!</p>
</div>

<script>
    $("p").live("click", function() {
        $(this).after("<p id='abcd123' class='abcd2323'>Another paragraph!</p>");
        var number = this.id.match(/^abcd(\d+)$/)[1];
        var iD = this.id;
        alert(number);
        alert(iD);
        var div = "#"+"aDiv"+number;
        alert(div)
        $(div).remove();
    });
</script>

</body>
</html>

这是上述代码的小提琴链接.

关于我在哪里做错的任何建议!!!!

Any suggestions about where i am doing it wrong!!!!

谢谢!

推荐答案

问题是您的Div与段落中的数字不同,应该是:

The problem is that your Div has a different number from the paragraph, it should be:

<div id="aDiv1234">
<p id="abcd1234">

否:aDiv123

这篇关于有什么方法可以使用JS变量代替jquery选择器"#id"作为输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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