简体   繁体   English

将jQuery选择器与Javascript函数一起使用会引发错误

[英]Using jQuery selectors with Javascript functions throwing errors

Trying to change from using jQuery for everything and playing around with more native javascript. 尝试从对所有内容都使用jQuery转变为使用更多本机javascript。 However, I keep getting problems when trying to use jQuery functions with native selectors and javascript functions with jQuery selectors. 但是,在尝试将jQuery函数与本机选择器一起使用以及javascript函数与jQuery选择器一起使用时,我总是遇到问题。

Below is a simple example about my problem (all were run separately): 以下是关于我的问题的简单示例(所有操作均单独运行):

var text = document.getElementById("text");
var text2 = $("#text");

text.style.color = "blue"; //this works    
text.css("color", "blue"); //throws text.css is not a function error

text2.css("color", "blue"); //this works
text2.style.color = "blue"; //throws Cannot set property 'color' of undefined error

I thought that jQuery was just javascript and that you could use native functions and methods with jQuery selectors and vice versa. 我以为jQuery只是JavaScript,您可以将本机函数和方法与jQuery选择器一起使用,反之亦然。 What am I missing? 我想念什么?

In your code text does not jQuery object. 在您的代码text中没有jQuery对象。 And it does not have css function. 而且它没有css功能。 Use JavaScripts methods in this case. 在这种情况下,请使用JavaScripts方法。

Non-jQuery objects has native functions only. 非jQuery对象仅具有本机功能。

UPD: also jQuery object does not have style property. UPD:jQuery对象也没有style属性。 So you call style.color and it equal to try to get color of undefined. 因此,您调用style.color ,它等于尝试获取未定义的color

There is no any .css() in the javascript; javascript中没有任何.css() and there is no any .style properties in the Jquery. 并且Jquery中没有任何.style属性。

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

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