Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

Syntax Urgent only one line | jquery


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have syntax errors in your code. You are using the "CSS Slector" syntax instead of the method. The other issue is you are expecting $(this) to be all the spans when it is just the one you clicked on.

var spans = $('.icon-appointment span');
spans.on("click", function () {
   var currentSpan = $(this);
   var prevIndex = currentSpan.index() - 1;
   var prevSpan = spans.eq(prevIndex);
   console.log(prevSpan.hasClass('active-icon-green'));
});

Now if they were siblings in the HTML, you can just use prev()

var spans = $('.icon-appointment span');
spans.on("click", function () {
   var currentSpan = $(this);
   var prevSpan = currentSpan.prev();
   console.log(prevSpan.hasClass('active-icon-green'));
});

And after the 4th edit....

The class is on the <i> not the span. So you would need to look at the <i>

var prevSpan = spans.eq(prevIndex);
var prevIcon = prevSpan.next("i");
console.log(prevIcon.hasClass('active-icon-green'));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...