jQuery 语法错误,无法识别的表达式:#[object HTMLElement] 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15026284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Syntax error, unrecognized expression: #[object HTMLElement] Error
提问by Denys Séguret
I made a Overlay section with jQuery.
我用 jQuery 做了一个 Overlay 部分。
This i call with <a href="javascript:slide(cms);">
.
But i get this error:
我用<a href="javascript:slide(cms);">
.
但我收到此错误:
Error: Syntax error, unrecognized expression: #[object HTMLElement] [http://localhost//js/jquery-1.9.1.min.js:4]
错误:语法错误,无法识别的表达式:#[object HTMLElement] [http://localhost//js/jquery-1.9.1.min.js:4]
Any idea? Here's the slide method:
任何的想法?这是幻灯片方法:
function slide(content) {
$('#' + content).show(0);
$('#' + content).animate({
left: '0%'
}, 500);
}
$('.c-close').click(function(){
$('.slide').animate({
left: '100%'
}, 500);
$('.slide').hide(0);
});
回答by Denys Séguret
content.toString()
is [object HTMLElement]
. So content
is an element (or the product of another bug in the part of the code we don't see).
content.toString()
是[object HTMLElement]
。所以content
是一个元素(或其他的bug在我们看不到的部分代码的产品)。
As content
is an element, not its id, you should use $(content)
, not $('#'+content)
.
作为content
一个元素,而不是它的 id,你应该使用$(content)
,而不是$('#'+content)
。
回答by Timmetje
The right answer depends on what content and cms is.
正确答案取决于内容和 cms 是什么。
You send cms because your element has an id with the name "cms"
您发送 cms 是因为您的元素具有名称为“cms”的 id
If content is an DOM element identifier you should make sure that Id is passed as a string.
如果 content 是 DOM 元素标识符,您应该确保 Id 作为字符串传递。
<div id="cms" ></div>
<a href="javascript:slide('cms');">
And rename your function so its more clear what it does:
并重命名您的函数,使其更清楚它的作用:
function slide(element_id) {
$('#' + element_id).show(0);
$('#' + element_id).animate({
left: '0%'
}, 500);
}
There are some other solutions:
还有一些其他的解决方案:
If cms is a previously set variable containing a DOm element.
如果 cms 是包含 DOm 元素的先前设置的变量。
For example.
例如。
Javascript:
Javascript:
var cms = $('cms');
function slide(content) {
content.show(0);
content.animate({
left: '0%'
}, 500);
}
OR:
或者:
var cms = document.getElementById("cms");
function slide(content) {
$(content).show(0);
$(content).animate({
left: '0%'
}, 500);
}
html:
html:
<a href="javascript:slide(cms);">.
Depending on what you want.
取决于你想要什么。
Remember if you use @dystroy his solution which is very good, not to simply send the element id as a variable. But actually send the dom element post-selection.
请记住,如果您使用@dystroy,他的解决方案非常好,而不是简单地将元素 id 作为变量发送。但实际上是在选择后发送 dom 元素。
My solution would be:
我的解决方案是:
<div id="cms"></div>
<a href="#" id="slide">slide</a>
<script>
(function(){
$('slide').on('click',function(e){
e.preventDefault();
var slideElement = $('div#cms');
slideElement.show(0);
slideElement.animate({
left: '0%'
}, 500);
});
})();
</script>
回答by Michael Schmidt
To use a variable in a jQuery selector, it should be a string.
So surround your cmswith ' --> <a href="javascript:slide('cms');">
要在 jQuery 选择器中使用变量,它应该是一个字符串。
所以用'-->包围你的cms<a href="javascript:slide('cms');">