简体   繁体   English

为什么jQuery在jSfiddle中起作用但在站点中不起作用?

[英]Why does jQuery work in jSfiddle but not site?

I am using jQuery to change the height of a div, I was using static values before that were set by me, but now I made one that makes the div to its full height and returns it to 24px, Now it works great in JSFiddle but not on the site. 我正在使用jQuery来更改div的高度,我使用的是我设置的静态值,但是现在我制作了一个使div达到其完整高度并将其返回到24px的值,现在它在JSFiddle中很好用,但是不在网站上。

jQuery(".dsellactiontext").toggle(function(){
jQuery(this.parentNode).animate({
    height: $(this.parentNode)[0].scrollHeight+'px'
  }, 400);
},function(){
jQuery(this.parentNode).animate({height:24},600);
}); 

The fiddle : http://jsfiddle.net/skoltyno/ZBR8G/ 小提琴: http : //jsfiddle.net/skoltyno/ZBR8G/

The site test page : http://bocaratonrealestatemarket.com/jquery-test-page/ 站点测试页面: http : //bocaratonrealestatemarket.com/jquery-test-page/

You can see in the inspector that the height is not being toggled to full but it will toggle it to 24px. 您可以在检查器中看到,高度未切换为全高,但会将其切换为24px。

Whats going on here? 这里发生了什么?

Wordpress runs jQuery in noConflict mode , meaning that $ is not aliased to jQuery . WordPressnoConflict模式下 运行jQuery ,这意味着$不别名为jQuery This means your $(this.parentNode)[0].scrollHeight+'px' throws an error (which you can see in the console). 这意味着您的$(this.parentNode)[0].scrollHeight+'px'引发错误(您可以在控制台中看到)。

Try; 尝试;

jQuery(this.parentNode)[0].scrollHeight+'px'

... or, as your creating a jQuery object of a DOM Element, then accessing the DOM Element immediately, just: ...,或者,当您创建DOM元素的jQuery对象时,立即访问DOM元素,只是:

this.parentNode.scrollHeight+'px'

... should suffice. ...就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM