0

谁能解释下面的代码?

function showImage(){
var imgs = $('#gallery img');
imgs.live('click', function(){
    var title = $(this).attr('title');
    $('#picture h1').text(title);
    $('#pic').html($(this).clone());

    $.mobile.changePage($('#picture'));
});

}

4

1 回答 1

0

该代码将单击事件处理程序附加到所有 img 元素,并在单击时显示单个 img。

// get all img elements in #gallery
var imgs = $('#gallery img');
// bind a click handler on all img elements
imgs.live('click', function(){
    // get the title attribute of the clicked img
    var title = $(this).attr('title');
    // set h1 text to title and add the clicked img to our new page
    $('#picture h1').text(title);
    $('#pic').html($(this).clone());
    // switch to the new page
    $.mobile.changePage($('#picture'));
});

我假设单个 img 页面如下所示:

<div data-role="page" id="picture">
    <div data-role="content">
    <h1></h1>
    <div id="pic"></div>
    </div>
</div>
于 2013-04-10T17:28:17.077 回答