如何将缩略图设为链接,标题附加到右侧?

时间:2015-10-15 03:54:10

标签: javascript jquery

想要将缩略图创建为链接,并将标题放在缩略图的右侧。

我尝试使用after并且还搞乱了HTML,看看我是否可以将它们彼此相邻。

我将如何这样做?

这是我的JS:

$(function(){
    var search = function(term, el){
        $.get('https://www.googleapis.com/youtube/v3/search', {
            part: 'snippet',
            key: 'AIzaSyA8Dm-N-QCxPgkvkwiI0LkY0aK4ZPGfP3c',
            q: term,
            maxResults: 25
        }, function(data){
            display(data.items, el);
        });     
    };

    var display = function(videos, el){
        el.empty();
        $.each(videos, function(index, video){
            var thumbnail = video.snippet.thumbnails.default.url;
            var vidTitle = video.snippet.title;
            var vidID = video.id.videoId;
            var container = $('<span/>', { 'class': 'video-container' });
            var thumbContainer = $('<div/>', { 'class': 'thumbnail' });
            var thumbnailElement = $('<img/>', { 'src': thumbnail });
            var thumbnailLink = $('<a/>', {'src': thumbnail});
                thumbContainer.append(thumbnailElement).append(thumbnailLink);

            var titleContainer = $('<span/>', { 'class': 'title' });
            var titleElement = $('<a/>', {
                    'href': 'https://www.youtube.com/watch?v='+vidID,
                'target': '_blank',
                'html': vidTitle,               
            });

            titleContainer.append(titleElement);

            container.append(thumbContainer).append(titleContainer);

            el.append(container);
        });
    };

    //jQuery event listeners
    $('.search-btn').on('click', function(event){
        event.preventDefault();
        var searchInput = $('.search-field').val();
        search(searchInput, $('#search-results'));
    });
});

这是我的HTML:

<body>
    <div class="container">
        <div class="master-nav">
            <form class="search-nav">
                <span>
                    <img src="https://www.thinkful.com/learn/static/shared/images/tf-logo-blue-t-1200x1200.png">
                </span>
                <span class="brand">
                    <h1>hinkful Tube</h1>
                </span>
                <span class="search-query"><input class="search-field">
                    <button class="search-btn">Submit</button>
                </span>
            </form>
        </div>
        <div id="search-container">
            <div id="search-results">
                <span class="video-container"></span>
                <span class="title"></span>
            </div>
        </div>  
    </div>
</body>

这是CSS:

.container {
    margin: 0 auto; 
    font-family: arial;
    color: #4628FB;
}

.master-nav {
    background-color: white;
    height: 4.375rem;
    width: 100%;
    border-bottom: 1px solid #4628FB;
    z-index: 9999;
}

.search-nav {
    width: 1000px;
    margin: 0 auto;
}

.search-query{
    float: right;
    padding: 10px 10px; 
}

.brand{
    float: left;
    padding-left: 0px;  
}

#search-results a {

}

#search-container { 
    width: 1000px;
    margin: 10px auto;
}

.search-nav img {
    width: 5%;
    float:left;
    padding-top: 10px
}

1 个答案:

答案 0 :(得分:1)

<!doctype html>
<html lang="en">
<head>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<style>
.video-container{display:inline-block; width:100%}
.thumbnail{float:left;}


.container {
    margin: 0 auto; 
    font-family: arial;
    color: #4628FB;
}

.master-nav {
    background-color: white;
    height: 4.375rem;
    width: 100%;
    border-bottom: 1px solid #4628FB;
    z-index: 9999;
}

.search-nav {
    width: 1000px;
    margin: 0 auto;
}

.search-query{
    float: right;
    padding: 10px 10px; 
}

.brand{
    float: left;
    padding-left: 0px;  
}

#search-results a {

}

#search-container { 
    width: 1000px;
    margin: 10px auto;
}

.search-nav img {
    width: 5%;
    float:left;
    padding-top: 10px
}
</style>

</head>
<body>
<div class="container">
        <div class="master-nav">
            <form class="search-nav">
                <span>
                    <img src="https://www.thinkful.com/learn/static/shared/images/tf-logo-blue-t-1200x1200.png">
                </span>
                <span class="brand">
                    <h1>hinkful Tube</h1>
                </span>
                <span class="search-query"><input class="search-field">
                    <button class="search-btn">Submit</button>
                </span>
            </form>
        </div>
        <div id="search-container">
            <div id="search-results">
                <span class="video-container"></span>
                <span class="title"></span>
            </div>
        </div>  
    </div>
<script type="text/javascript">
$(function(){
    var search = function(term, el){
        $.get('https://www.googleapis.com/youtube/v3/search', {
            part: 'snippet',
            key: 'AIzaSyA8Dm-N-QCxPgkvkwiI0LkY0aK4ZPGfP3c',
            q: term,
            maxResults: 25
        }, function(data){
            display(data.items, el);
        });     
    };

    var display = function(videos, el){
        el.empty();
        $.each(videos, function(index, video)
        {
            var thumbnail = video.snippet.thumbnails.default.url;
            var vidTitle = video.snippet.title;
            var vidID = video.id.videoId;

            var element = "<span class='video-container'><div class='thumbnail'><a href='https://www.youtube.com/watch?v="+vidID+"' target='_blank'><img src='"+thumbnail+"'></a></div><span class='title'>"+vidTitle+"</span></span>";
            el.append(element);
        });
    };

    //jQuery event listeners
    $('.search-btn').on('click', function(event){
        event.preventDefault();
        var searchInput = $('.search-field').val();
        search(searchInput, $('#search-results'));
    });
});
</script>

</body>
</html>