混合Javascript和jQuery? [英] Mixing Javascript and jQuery?
问题描述
主要问题:是否可以使用JavaScript条件然后使用一些jQuery代码?
Main Question: Is it possible to use a JavaScript conditional and then use some jQuery code?
这是一个当前不工作的例子:
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>
JavaScript条件由我的电子商务平台(Volusion)提供,仅针对类别页面(jQuery是我这样做。我问他们这个JavaScript代码是否仍然有效,因为我的网址不包含/SearchResults.asp(其中一个实际上是/Meats-s/3393.htm),但是他们向我保证它仍然有用。
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.
次要问题:location.pathname.indexOf行有什么作用?
Secondary Question: What do the "location.pathname.indexOf" lines do?
推荐答案
jQuery和JavaScript不是单独的语言。 jQuery是一个用Javascript编写的库,所以任何有效的jQuery语句都是有效的JavaScript语句。
jQuery and JavaScript are not separate languages. jQuery is a library written in Javascript, so any valid jQuery statement is a valid JavaScript statement.
Q2:
location.pathname
是您的域名后网址的路径。
Q2:
location.pathname
is the path of the url after your domain name.
例如 http://www.google.com/s/goo.html - >路径名将返回s / goo.html
e.g http://www.google.com/s/goo.html -> the pathname would return s/goo.html
所以你的if条件试图找出这个字符串是否包含子字符串-s。
So your if condition is trying to find out if this string contains the substring "-s".
这篇关于混合Javascript和jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!