简体   繁体   English

使用Jquery获取单击的项目attr

[英]Getting clicked item attr using Jquery

<aside class="widget">
<h6 class="widget-title">Play List</h6>

<div class="newsletter">
<div class="nwlwrap">

<article class="post-item">
    <div class="featured-image clearfix" > 
        <a href="http://vimeo.com/46163823" rel="prettyPhoto" class="link-thumbnails-video">    
        <img alt="" src="images/thumbs/video-1.jpg" class="thumbnails link-thumbnails-video">
        </a>

        <div class="image-caption">
            <span>Posted 2012/07/18&nbsp;</span>
        </div>
    </div>
    <div class="entry-content">
        <h3 class="entry-title"><a href="#">What is Kopatheme Layout Manager</a></h3>
        <p>Vivamus. Sagittis facilisis hymenaeos scelerisque ad scelerisque. Massa felis odio rhoncus ligula sollicitudin Magna. Laoreet. Vel tristique tellus nam quis curae</p>
    </div>
</article>


<div class="clear"></div>
<div class="kp-divider"></div>

</div>
</div>
</aside>

$(".widget").click(function(event){
         $(this).find('.featured-image clearfix img').attr('id');
         alert("hey");
    });

I have a list of videos, I want when user clicks on one of the videos, then the clicked video, start playing in the video player. 我有一个视频列表,我希望当用户点击其中一个视频,然后点击的视频,开始在视频播放器中播放。 the jquery clicked function is not working, Kindly guide me in right direction. jquery点击功能不起作用,请指导我正确的方向。

<div class="featured-image clearfix" >

That would be one element with two classes, and you'd target that like this: 这将是一个包含两个类的元素,你可以像这样定位:

$(".widget").click(function(event){
     $(this).find('.featured-image.clearfix img').attr('id');
     alert("hey");
});

using a space between the classes looks for something like: 在类之间使用空格查找类似于:

<div class="featured-image"><div class="clearfix"></div></div>

Now if the image only had an ID, you'd get it ? 现在如果图像只有一个ID,你会得到它吗?

@Desire your JavaScript code isn't wrapped in a script tag. @Desire您的JavaScript代码未包含在脚本标记中。 The fact that you haven't wrapped it, makes me wonder if you have jQuery included in you page at all? 你没有包装它的事实让我想知道你的页面中是否包含jQuery? Also, as @adeneo points out, your object doesn't have any ID. 此外,正如@adeneo指出的那样,您的对象没有任何ID。

So wrap your script in script tags, and either make sure your object has an ID or find some other way to identify it. 因此,将脚本包装在脚本标记中,并确保您的对象具有ID或找到其他方法来识别它。 Also, you don't need to write: 此外,您不需要写:

$(this).find('.featured-image.clearfix img').attr('id');

Give the div and ID, it is a lot easier and will process a lot faster. 提供div和ID,它更容易,并且处理速度更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM