简体   繁体   English

jQuery选择器属性值空格问题

[英]jQuery selector attribute value white space problem

A couple weeks ago, i've been using this code to make the scroll event into a specific page, and was working very good.几周前,我一直在使用此代码将滚动事件变为特定页面,并且效果很好。

But, yesterday, the client asked to change de attribute name from "Ouro" to "Ouro Envelhecido".但是,昨天,客户要求将属性名称从“Ouro”更改为“Ouro Envelhecido”。

The first functional code was like that:第一个功能代码是这样的:


jQuery(document).ready(function($) {
    $("[data-title=Ouro]").click(function() {
        $('html, body').animate({
            scrollTop: $(".custom-product-page").offset().top - 100
        }, 1000);
    })

And after the attribute name change, we just added the " Envelhecido", like this:在属性名称更改后,我们只是添加了“Envelhecido”,如下所示:


jQuery(document).ready(function($) {
    $("[data-title=Ouro Envelhecido]").click(function() {
        $('html, body').animate({
            scrollTop: $(".custom-product-page").offset().top - 100
        }, 1000);
    }) 

And the code stop working.并且代码停止工作。 At the console, I got the warning saying Uncaught Error: Syntax error, unrecognized expression: [data-title=Ouro Envelhecido], but, the attribute name is 100% correct.在控制台,我收到警告说 Uncaught Error: Syntax error, unrecognized expression: [data-title=Ouro Envelhecido],但是,属性名称是 100% 正确的。

imgs: https://prnt.sc/13py6mv https://prnt.sc/13py8zy图片: https://prnt.sc/13py6mv https://prnt.sc/13py8zy

Any help would be appreciated!任何帮助,将不胜感激!

You should use quotes to wrap your data-title value您应该使用引号来包装您的数据标题值

jQuery(document).ready(function($) {
    $("[data-title='Ouro Envelhecido']").click(function() {
        $('html, body').animate({
            scrollTop: $(".custom-product-page").offset().top - 100
        }, 1000);
    }) 
})

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

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