在写一个todoList作品时,需要点击事件更改图片路径时,遇到了获取不到图片路径的情况
在html中用src=“”来获取的图片,凭主观臆想觉得在js中判断它的路径时,没有作用,控制台中显示的不是我们熟悉的路径格式
查阅得知,这是绝对路径
console.log(this.src); //获取的图片的绝对路径
console.log(this.getAttribute("src")); //获取的是图片的相对路径
let todoFinish = todoPart.querySelector('.todo-finish');
let starImg = todoPart.querySelector('.star-img');
todoFinish.onclick = function () {
console.log(todoFinish);
console.log(this.src);
// if (this.src == './images/circle.png') {
if (this.getAttribute("src") == './images/circle.png') {
this.setAttribute('src', './images/right.png');
starImg.setAttribute('src','./images/stary.png');
} else {
this.setAttribute('src', './images/circle.png');
}
更改一下
获取到啦