设置默认字符串值

时间:2013-06-04 02:57:06

标签: javascript jquery

对不起伙计们,我认为这是直截了当的,但我只是没有得到结果而且一直在尝试/搜索年龄!

我希望在没有图片的标题/标题时在幻灯片中设置默认标题(即“没有字幕数据”)。我怎样才能做到这一点?脚本在下面,谢谢你看看,并抱歉成为新手。

(这与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;


}

3 个答案:

答案 0 :(得分:0)

你的意思是:

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

更改为

$title = ($caption.length) ? $caption[0].innerHTML : "There is no caption data";

答案 1 :(得分: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属性< strong> BEFORE 您初始化您的fancybox。有点像:

$('.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初始化时都有一个标题。

答案 2 :(得分:0)

是的,向所有人道歉......我发现PWI脚本的位置越来越近,并且在底部附近可以看到附加了一点,这似乎有用......而且下层脚本似乎完全是多余的? / p>

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;