When you pass 2 variables in the jQuery selector, there is no result, but when …

for example:

var a = “1234”;

var b = “line1\\.5”;

now this line of code:

“#” + a + b;

puts out this string

“#1234line1\.5”

and when I enter it in the selector like this:

$(“#1234line1\.5”)

it shows the correct element

but $(“#” + a + b)

Does not

Thanks in advance for any suggestions

I’m surprised it works in the console, the id
selector is invalid. CSS
id
selectors cannot start with a digit

. You can escape it, though:

var a = "\31 234"; // \31 = 1, then you need the space to terminate the escape

var b = "line1\.5";        // Removed a pair of \ from this, I assume you
                            // don't have a backslash in the id

$("#" + a + b).html("Found it");

This works, for instance: Live Example

(function() { "use strict"; var a = "\31 234"; var b = "line1\.5"; $("#" + a + b).html("Found it"); })();

If you really have a backslash in the id
, the escape for a backslash is 5c
, which you write in a string literal as \5c
, so: Live Example

(function() { "use strict"; var a = "\31 234"; var b = "line1\5c \.5"; $("#" + a + b).html("Found it"); })();
Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 前端开发 » When you pass 2 variables in the jQuery selector, there is no result, but when …

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录