jQuery无法识别前缀为“/”的类名
16lz
2021-01-22
I have the following html code:
我有以下HTML代码:
<div class="/temp">asdf</div>
<input type="button" class="hide_message_button" value="Hide">
And the jQuery for it is something like this:
而jQuery就是这样的:
$('.hide_message_button').click(function(){
var bool = $('./temp').is(':hidden');
if(bool){
$('./temp').show();
$(this).val('Hide');
}
else {
$('./temp').hide();
$(this).val('Show');
}
});
Well all I am trying to do is show or hide the div on button click. However, I get the following error:
好吧,我要做的就是在按钮点击时显示或隐藏div。但是,我收到以下错误:
Uncaught Error: Syntax error, unrecognized expression: ./temp
Works fine when the class name is just "temp". Unfortunately, the class name is prefixed with / and I would not like to change it. How do I resolve the error?
当类名只是“临时”时工作正常。不幸的是,类名前缀为/,我不想改变它。我该如何解决这个错误?
3 个解决方案
#1
2
You need to escape the meta-characters.
你需要逃避元字符。
To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \\
要使用任何元字符(例如!“#$%&'()* +,。/:; <=>?@ [] ^`{|}〜)作为名称的文字部分,必须使用两个反斜杠进行转义:\\
$('.\\/temp').is(':hidden');
更多相关文章
- [求助]如何用JQuery来实现Ctrl+Space完成输入特定字符提示的自动
- 在线请教调用Jquery错误:TypeError: a is undefined 的错误原因
- 在发出xml Ajax请求时获取错误412
- 如何设置请求标头字符串[重复]
- Internet Explorer导致无效的真实性令牌错误
- DataTables警告:table id = DataTables_Table_0 - Ajax错误。有
- 如何在使用jquery验证和自定义错误放置时清除错误
- Ajax调用php脚本返回404错误
- Jquery验证插件,获取错误字符串