0

申し訳ありませんが、これは簡単なことだと思いますが、結果が得られず、年齢を試したり検索したりしています!

写真にキャプション/タイトルがない場合に、スライドショーにデフォルトのキャプション (つまり、「キャプション データがありません」) を設定しようとしています。これどうやってするの?スクリプトを以下に示します。ご覧いただきありがとうございます。初心者で申し訳ありません。

(これは、PWI (Picasa Web インテグレーター)、jquery 1.8.3、fancybox 2.1.4 と連携しています。

テスト ページもhttp://www.talesfromthesaddle.com/photosTEST/photos.shtmlにあります。

// This function is called by FancyBox to format the title of a picture
function formatPhotoTitleFancyBox() {
    var $title = this.element.title;
        this.title = $title;

    return;
    if (this.element.parentNode.childNodes && (this.element.parentNode.childNodes.length > 1)) {
        var $caption = $(".captiontext", this.element.parentNode);

        if ($caption.length > 0) {
            $title = $caption[0].innerHTML;
        }
        var $links = $(".downloadlink", this.element.parentNode);
        if ($links.length > 0) {
            var downloadLink = '<a style="color: #FFF;" href="' + $links[0].href + '">Download</a>';
            $title = $title + '&nbsp;&nbsp;' + downloadLink;
        }
    }



title = $title;


}
4

3 に答える 3

0

jquery を完全に受け入れます。これが私があなたの関数を書き直す方法です

// This function is called by FancyBox to format the title of a picture
function formatPhotoTitleFancyBox()
{
    var $elem = $(this.element);
    var title = $elem.attr('title') || 'No Caption'; // use || operator to set a default

    var caption = $elem.parent().find(".captiontext").html();
    title = caption || title; // if no html() found or no element found, default back to title

    var $links = $elem.parent().find(".downloadlink").first();
    if ($links.length > 0)
    {
        var downloadLink = '<a style="color: #FFF;" href="' + $links.attr('href') + '">Download</a>';
        title = title + '&nbsp;&nbsp;' + downloadLink;
    }

    // finally, after ALL the edits to title, set this.title.  setting it above like in the OP question means all future changes are missed out on.

    this.title = title;

}

編集:OPのコメントの更新を考えると、私が行うかもしれない提案は次のとおりです:関数を使用して設定する代わりに、ファンシーボックスthis.titleを初期化する前に、すべての要素に更新されたtitle属性があることを確認してください。次のようなもの:

$('.the-same-selector-for-fancybox').each(function()
{
    var $elem = $(this);
    var title = $elem.attr('title') || 'No Caption'; // use || operator to set a default

    var caption = $elem.parent().find(".captiontext").html();
    title = caption || title; // if no html() found or no element found, default back to title

    var $links = $elem.parent().find(".downloadlink").first();
    if ($links.length > 0)
    {
        var downloadLink = '<a style="color: #FFF;" href="' + $links.attr('href') + '">Download</a>';
        title = title + '&nbsp;&nbsp;' + downloadLink;
    }

    // finally, after ALL the edits to title, set this.title.  setting it above like in the OP question means all future changes are missed out on.

    $(this).attr('title', title);
});

// call fancybox init here

このようにすることで、fancybox が初期化されるまでに、各要素にタイトルがあることを確認できます。

于 2013-06-04T03:11:41.437 に答える
0

あなたは次のようなことを意味します:

if ($caption.length > 0) {
    $title = $caption[0].innerHTML;
}

への変更

$title = ($caption.length) ? $caption[0].innerHTML : "There is no caption data";
于 2013-06-04T03:10:39.583 に答える
0

そうです、すべての人に申し訳ありません....私が見つけて追加したPWIスクリプトをさらに調べて、下部近くに見られるように、これは機能しているようです....そして、下部のスクリプトは完全に冗長なようですか?

function photo(j, hidden, username) {
            var $html, $d = "", $c = "", $youtubeId = "", $caption;
            if (j.summary) {
                var $matched = j.summary.$t.match(/\[youtube\s*:\s*(.*)\s*\](.*)/);
                if ($matched) { // Found youtube video entry
                    $youtubeId = $matched[1];
                    $c = $matched[2].replace(/[\r\n\t\s]+/g, ' ');
                    $caption = $matched[2].replace(/[\n]/g, '<br/>');
                } else {
                    $c = j.summary.$t.replace(/[\r\n\t\s]+/g, ' ');
                    $caption = j.summary.$t.replace(/[\n]/g, '<br/>');
                }
            }
            if (settings.showPhotoFilename) {
                if ($caption.length > 0) {
                    $caption += ", ";
                }
                $caption += settings.labels.fileName + " " + j.media$group.media$title.$t;
            }
            if (settings.showPhotoDate) {
                if ((j.exif$tags) && (j.exif$tags.exif$time)) {
                    $d = formatDateTime(j.exif$tags.exif$time.$t) + " ";
                }
            }
            var $pretitle = $c.replace(new RegExp("'", "g"), "&#39;");
            $title= $pretitle||'There is no caption'; //My appended line
            $d += $title;
于 2013-06-04T15:48:39.773 に答える