开发者

How do I write a particular line of Javascript into Jquery syntax?

I am trying to write a skp nav link script but instead of grabbing the id i want to grab the class. How can I write the following script in jquery syntax to get the class name?

$(document).ready(function() {
    $(".ajax-video").click(function(event) {
    开发者_Go百科    event.preventDefault();
        document.getElementById("currently-playing-title").scrollIntoView();
    });
});

Also for some reason the following does not work:

$('.videoplayer').scrollIntoView();


try

$('.videoplayer')[0].scrollIntoView();

$('.videoplayer') a jQuery collection and so you can only run jQuery functions on it. [0] accesses the first DOM element in that collection.

Note that if you have more than one element with the videoplayer class, you may get unexpected results here. It's honestly better to use the element ID.

If you have multiple elements with .videoplayer, but only one is visible at a time (tabs, etc), you can also try:

$('.videoplayer:visible')[0].scrollIntoView()


The following shows how to obtain the class attribute via the identifier:

var className = $('#currently-playing-title').attr('class');
$('.' + className).scrollIntoView();


Sorry, I'm not able to make a comment on cwolves' answer, but jQuery.get() is preferred usage for fetching the DOM ref.

$('.videoplayer').get(0).scrollIntoView();

Using [0] works too, but using .get() results in cleaner looking code, just slightly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜