Javascript 什么';a[href^=";和a[href*=\\\\\\\\]
两者之间有什么区别Javascript 什么';a[href^=";和a[href*=\\\\\\\\],javascript,dom,css-selectors,Javascript,Dom,Css Selectors,两者之间有什么区别 'a[href^="#"]' 及 我想创建一个影响网站所有内部链接的平滑滚动javascript 这是完整的剧本 jQuery(document).ready(function($) { $('a[href^="#"]').bind('click.smoothscroll',function (e) { e.preventDefault(); var target = this.hash, $target = $(tar
'a[href^="#"]'
及
我想创建一个影响网站所有内部链接的平滑滚动javascript
这是完整的剧本
jQuery(document).ready(function($) {
$('a[href^="#"]').bind('click.smoothscroll',function (e) {
e.preventDefault();
var target = this.hash,
$target = $(target);
$('html, body').stop().animate( {
'scrollTop': $target.offset().top-40
}, 900, 'swing', function () {
window.location.hash = target;
} );
} );
} );
是“属性以开头”CSS选择器^=
是“属性包含”CSS选择器*=
\\
在两种上下文中转义:
不是有效的CSS选择器;需要是a[href*=#]
a[href*=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
需要在JavaScript字符串文本中转义自身\
let a='a[href^=“#”];
设b='a[href*=\\\\\\\]';
设c='a[href*=“#”];
设d='a[href*=#]';
console.log(a,document.queryselectoral(a.length);
console.log(b,document.queryselectoral(b).length);
console.log(c,document.queryselectoral(c.length));
试一试{
文件.查询选择全部(d);
}捕捉(错误){
控制台日志(错误消息);
}
对不起,我没有要求的任何信息。^=
查找以给定字符串开头的属性值*=
查找包含给定字符串的属性。从该页面:“[attr^=value]
表示属性名为attr的元素,其值以value作为前缀(前面是value)。”以及更高版本,“[attr*=value]
表示属性名为attr的元素,其值在字符串中至少包含一个值。“我认为OP要求\\\\\\\
(我相信这是css\\\\\\\\\\\\\
)和\\\
非常感谢你,你救了我,我已经测试了一个[href*=\\\\\\\\\\\\\\\\\]它工作正常。我在回答中添加了一个说明。反斜杠不是唯一的方法,你可以像第一个选择器一样引用。对不起,我英语说得不太好,而且我是Javascript的新手,请你重写我的代码以获得最佳选择器,这将节省我很多时间,谢谢。
jQuery(document).ready(function($) {
$('a[href^="#"]').bind('click.smoothscroll',function (e) {
e.preventDefault();
var target = this.hash,
$target = $(target);
$('html, body').stop().animate( {
'scrollTop': $target.offset().top-40
}, 900, 'swing', function () {
window.location.hash = target;
} );
} );
} );