【问题标题】:Get elements from other class using jQuery (Prettyphoto)使用 jQuery (Prettyphoto) 从其他类获取元素
【发布时间】:2012-09-03 12:32:34
【问题描述】:

我在使用 jQuery 和 prettyPhoto(wordpress 插件)时遇到问题!我的问题是我想从已经工作的标签之外的隐藏 div 获取图像描述。这是我到目前为止所拥有的:

<div class="portfolie_short_content_containe">                  
    <a class="lightbox" style="text-decoration: none;" rel="lightbox" href="<?php echo $image[0]; ?>" title="<?php echo $image_desc; ?>">
        <span class="hover">
            <div class="portfolio_thump_hover_text">
                <span class="lable"><?php echo $short_descr;?></span><br>
                <span class="lable"><?php echo $link;?></span><br>
                <span class="lable"><FONT style="color : #f47b42;">Se CV</FONT></span>
            </div>
        </span>
        <img src="<?php echo $image[0]; ?>" width="<?php echo $image[1]; ?>" alt="<?php echo $image_alt; ?>"></img>
    </a>

    <!-- hidden div containing description -->
    <div id="lightbox" style="display: none"><p><?php echo $full_descr; ?></p><div>
</div>

jQuery.prettyphoto.js 文件看起来像这样!

theRel = $(this).attr('rel');
galleryRegExp = /\[(?:.*)\]/;
isSet = (galleryRegExp.exec(theRel)) ? true : false;
var x = 500;
var elmtid = $(this).attr('class');

// Put the SRCs, TITLEs, ALTs into an array.
pp_images = (isSet) ? jQuery.map(matchedObjects, function(n, i) {
    if ($(n).attr('rel').indexOf(theRel) != -1) 
        return $(n).attr('href'); 
}) : $.makeArray($(this).attr('href'));

pp_titles = (isSet) ? jQuery.map(matchedObjects, function(n, i) { 
    if ($(n).attr('rel').indexOf(theRel) != -1) 
        return ($(n).find('img').attr('alt')) ? $(n).find('img').attr('alt') : "";
}) : $.makeArray($(this).find('img').attr('alt'));

//Get text from hidden dir!     
pp_descriptions = (isSet) ? jQuery.map(matchedObjects, function(n, i) {
    if ($(n).attr('rel').indexOf(theRel) != -1) 
        return ($(n).find('p').text()) ? $(n).find('p').text() : ""; 
}) : $.makeArray($(this).find('p').text());

我对 jQuery 没有那么丰富的经验。任何人都可以帮助掌握描述吗?我已经尝试过这样的事情!

pp_descriptions = (isSet) ? jQuery.map(matchedObjects, function(n, i) {
    if($(n).attr('class') != -1) 
        return ($('#'+elmtid).html()) ? $('#'+elmtid).html()  : \"\"; 
}) : $.makeArray($('#'+elmtid).html());

但是没有用。

【问题讨论】:

  • 请为您的代码提供一些上下文。比如js代码第一行的this是指什么?您可以在 jsfiddle.net 中创建一个工作示例。
  • this 指的是 jQuery 对象,不是我的代码,而是 prettyphoto 插件的一部分,当按下链接时,它会在主页上创建一个灯箱,在这个框中我想要加载一些文本,但是这个文本有里面的链接弄乱了
  • 哦,我明白了。代码的最后一个 sn-p 中 elmtid 的值是多少?输出的 HTML 是什么样的(没有 PHP 代码,一旦呈现)?我认为这将有助于了解您遇到的问题。
  • Hmm tryid using a (debugger;) and the value of elmtid is "lightbox" if iam don't add PHP text area is blank

标签: php jquery wordpress prettyphoto


【解决方案1】:

解决了,可能很简单,但第一次使用 jQuery!

<script type="text/javascript">
var $j = jQuery.noConflict();
$j(document).ready(function(){

//reference the class instead of a tag
$j("div[class^='portfolie_short_content_containe']").prettyPhoto({theme: '<?php echo $portfolio_options["prtfl_prettyPhoto_style"]; ?>'});
});
</script>

在 js 文件中我改变了这个。

theRel = $(this).find('a').attr('rel');
//theRel = $(this).attr('rel');
galleryRegExp = /\[(?:.*)\]/;
isSet = (galleryRegExp.exec(theRel)) ? true : false;
var x=500;

pp_images = $.makeArray($(this).find('a').attr('href'));
pp_titles = $.makeArray($(this).find('img').attr('alt'));
pp_descriptions =  $.makeArray($(this).find('p').html());

它并不完美,但它可以帮助其他有同样问题的人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    相关资源
    最近更新 更多