【问题标题】:Loading content from Wordpress posts dynamically from clicked images从点击的图像动态加载来自 Wordpress 帖子的内容
【发布时间】:2011-09-21 23:07:08
【问题描述】:

单击图像时,我正在尝试将单独的 Wordpress 帖子中的内容插入到信息框中。我正在为网站的这一部分使用修改后的模板,其中图像是从数组中填充的。

我已经成功地使用带有 jQ​​uery 和静态 URL 的 .load() 方法完成了这项工作。

        <script type="text/javascript">
        jQuery(document).ready(function() {
                jQuery('.dynagrid a').click(function(event){ 
                // stop normal link click
                event.preventDefault(); 
                }); 
            jQuery('.dynagrid a').click(function(){
                jQuery('div.project-info').fadeToggle(function(){
                    jQuery(".infotext").load('/cowshed/akg .infotext > *', function() {
                        jQuery('div.project-info').fadeToggle() 
                    }); 
                });
            });
        }); 

我的问题在于使每张图片加载的 URL 都是动态的——每张图片加载不同的信息。我的计划是获取每个图像的&lt;alt&gt; 信息作为变量并插入它,例如jQuery(".infotext").load('/cowshed/+alt+ .infotext &gt;*'); 并将其与所需帖子的永久链接相匹配,但我尝试的一切都失败了。

我已经为此寻找了几个小时的解决方案,但我担心我在 jQuery 方面的经验不足会使这比实际情况更难。

这是目前的页面:Cowshed Tools

任何人都可以看到解决方案并提供一些启示吗?

【问题讨论】:

    标签: jquery ajax wordpress


    【解决方案1】:
     jQuery(".infotext").load('/cowshed/akg .infotext > *', function() {
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^
    

    指示的部分应该是提供您正在加载到 .infotext 元素中的内容的 URL。你得到了一个看起来像 url 和 CSS 选择器的混杂的东西,这不是一个有效的 URL。鉴于您想动态加载内容,它应该是:

    .load('http://example.com/fetchcontent?contentID=' + this.alt, function () ....);
    

    网址不必像那样是绝对的,但它应该是您服务器的有效网址。

    【讨论】:

    • 哈哈谢谢你的诚实。这似乎是有道理的。 + this.alt, 部分是否要求“alt”已经是包含相关图像替代文本的变量?我尝试var imgAlt = $(this).find('img').attr("alt", function(){ 在单击图像时发生,但这似乎破坏了我在图像上完美工作的preventDeafault 功能。任何进一步的帮助将不胜感激。
    猜你喜欢
    • 2014-06-15
    • 2020-05-07
    • 2017-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-24
    • 2013-12-08
    • 1970-01-01
    相关资源
    最近更新 更多