element.classList返回一个类数组,它understanding .includes()与数组一起使用,所以我不明白为什么这不起作用,我知道我可以使用.contains()和classList,但我很好奇为什么.includes()没有工作.
两者都是数组,如果我输入这个例如它不会工作
var li=document.createElement('li');
li.classList.add('main-nav');
li.classList.includes('main-nav');
Run Code Online (Sandbox Code Playgroud)
但这会
var ary=['a','b','c'];
ary.includes('a');
Run Code Online (Sandbox Code Playgroud) 我传递了一个对象作为方法中的第二个参数Object.create,但是我收到以下错误:
未捕获的TypeError:属性描述必须是对象:1
这是错误的代码:
var test = Object.create(null, {
ex1: 1,
ex2: 2,
meth: function () {
return 10;
},
meth1: function () {
return this.meth();
}
});
Run Code Online (Sandbox Code Playgroud) 我确定这是一个新手问题,但我不明白.each()普通$()选择器有什么好处.该$()选择什么样的选择是,适用什么都被应用到了选择它的所有匹配的情况下,所有实例.我的直接反应是:很好地.each()允许更复杂的事情,但你可以使用jquery方法链,所以我也可以使用普通选择器做更复杂的事情.例如,如果我有5个p元素,我写:
$("p").css("color","blue");//this would be applied to all five p elements
$("p").each(function(){$(this).css("color","blue")});//this does the
//same thing
Run Code Online (Sandbox Code Playgroud)
我确定有一个用途.each(),我目前只是看不到它,正常的选择器似乎已经遍历了所有内容.有人可以举例说明我为什么要使用它.each()吗?
我有一个简单的HTML文档,其中包含一个img元素和一组img src值.我还有一个脚本,img每次单击时都会将src 更改为另一个图像.它工作正常,但我注意到你必须点击最后一次图像两次才能重置图像数组.为什么我需要点击两次?
<img src="image1.jpg">
<script>
let images = ["image1.jpg", "image2.jpg", "image3.jpg", "image4.jpg", "image5.jpg"];
let imgIndex = 0;
$("img").click(function() {
if (imgIndex === images.length) {
imgIndex = 0;
} else {
imgIndex++
}
$(this).attr("src", images[imgIndex]);
});
</script>
Run Code Online (Sandbox Code Playgroud) 我创建了一个函数,该函数使用字符串将字符串转换为数组,然后将每个字符串字符与数组中的每个元音进行比较,如果匹配,则删除该字符串字符。在更长的单词上似乎无法正常工作。例如,使用“ tom”将删除o,但使用“ johnson”将仅删除第一个o,然后在末尾也删除n。我没有看到这个问题。
function removeVolwels(string){
let strAr= function(string){
//converts to to lower case and turns string into an array
let s= string.toLowerCase();
let strAr= s.split("");
return strAr;
}(string);
//stores vowels
let vowels=["a","e","o","i","u","y"];
//loops through each character
for(let i=0; i < string.length -1; i++){
console.log("i index " + i);
//compares each character in the string to a every vowel until it matches one
for(let j=0; j < vowels.length; j++){
console.log("j index " + j + " and " +vowels[j]);
if(string[i] === …Run Code Online (Sandbox Code Playgroud)