混合使用 Javascript 和 jQuery?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17582383/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:52:31  来源:igfitidea点击:

Mixing Javascript and jQuery?

javascriptjquery

提问by damon

Main Question: Is it possible to use a JavaScript conditional and then use some jQuery code?

主要问题:是否可以使用 JavaScript 条件,然后使用一些 jQuery 代码?

Here is a currently non-working example:

这是一个当前无法工作的示例:

<script>
//<![CDATA[ 
if (location.pathname == "/SearchResults.asp" 
|| location.pathname.indexOf("-s/") != -1 
|| location.pathname.indexOf("_s/") != -1) 
$('.colors_productname span').css("background-color", "#F7F7F7");
//]]> 
</script>

The JavaScript conditional was provided by my ecommerce platform (Volusion) to target just category pages (the jQuery was my doing). I asked them if this JavaScript code would still work given that my urls dont include "/SearchResults.asp" (One of them is actually "/Meats-s/3393.htm"), but they assured me that it should still work.

JavaScript 条件是由我的电子商务平台 (Volusion) 提供的,用于仅定位类别页面(jQuery 是我做的)。我问他们这个 JavaScript 代码是否仍然有效,因为我的网址不包含“/SearchResults.asp”(其中一个实际上是“/Meats-s/3393.htm”),但他们向我保证它仍然有效。

Secondary Question: What do the "location.pathname.indexOf" lines do?

次要问题:“location.pathname.indexOf”行有什么作用?

回答by Jeremy J Starcher

While you can mix then, it is important to understand that jQuery and native Javascript refer to DOM elements differently.

虽然您可以混合使用,但重要的是要了解 jQuery 和本机 Javascript 以不同方式引用 DOM 元素。

Assume you have a document with an element like this:

假设您有一个包含如下元素的文档:

<span id="message_location"><span>

To get that element in regular Javascript would be:

要在常规 Javascript 中获取该元素将是:

var spanElement = document.getElementById("message_location");

However, with the jQuery library you would use this syntax:

但是,对于 jQuery 库,您将使用以下语法:

var $spanElement = $("#message_location");

Here you mustbe aware that spanElementand $spanElementare notthe same thing. One is a DOM element and the other is an array cum jQuery object that holds one value -- the span element.

在这里,你必须知道spanElement$spanElement一样的东西。一个是 DOM 元素,另一个是包含一个值的数组和 jQuery 对象——span 元素。

When going back and forth from the jQuery library and regular Javascript, it is very easy to get confused as to what you are really referring to.

从 jQuery 库和常规 Javascript 来回切换时,很容易混淆您真正指的是什么。

(Personally, any time I have a jQuery reference to a DOM element, I always start the variable name with a $. It keeps my life simple...)

(就我个人而言,每当我有 jQuery 对 DOM 元素的引用时,我总是以 开始变量名$。它让我的生活变得简单......)

回答by mohkhan

jQuery and JavaScript are not separate languages. jQuery is a library written in Javascript, so any valid jQuery statement is a valid JavaScript statement.

jQuery 和 JavaScript 不是独立的语言。jQuery 是一个用 Javascript 编写的库,因此任何有效的 jQuery 语句都是有效的 JavaScript 语句。

Q2: location.pathnameis the path of the url after your domain name.

Q2: location.pathname是你的域名后面的url路径。

e.g http://www.google.com/s/goo.html-> the pathname would return s/goo.html

例如http://www.google.com/s/goo.html-> 路径名将返回 s/goo.html

So your if condition is trying to find out if this string contains the substring "-s".

所以你的 if 条件试图找出这个字符串是否包含子字符串“-s”。

回答by Pete Scott

You can absolutely mix them.

你绝对可以混合它们。

The location.pathname.indexOf is searching the current URL for an instance of "_s/" or "-s/". If the URL does not contain those characters (or if it equals "/SearchResults.asp"), your jquery code is run.

location.pathname.indexOf 正在搜索当前 URL 中的“_s/”或“-s/”实例。如果 URL 不包含这些字符(或者它等于“/SearchResults.asp”),则运行您的 jquery 代码。

回答by Seano666

You should be able to mix the two freely, be careful of using JQuery selectors vs regular Javascript dom elements though, the two are not always the same. (ie. class, multiple selectors.)

您应该能够自由地混合使用这两者,但要小心使用 JQuery 选择器与常规 Javascript dom 元素,但两者并不总是相同的。(即类,多个选择器。)

回答by mquandalle

jQuery is just a javascript object that provide usefull methods. So yes, you can use vanilla js and jQuery together.

jQuery 只是一个提供有用方法的 javascript 对象。所以是的,您可以一起使用 vanilla js 和 jQuery。