从jQuery load()结果剥离HTML [英] Stripping HTML from jQuery load() result

查看:62
本文介绍了从jQuery load()结果剥离HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是针对此问题的解决方案的后续。



我使用jQuery的 load()函数将div标签中的标题从一个页面拉到另一个页面我的网站。这很好用。



问题是, load() 标签本身,我不想要,因为它然后通过像源页面的CSS格式化。



这是PHP:

 函数get_team_articles($ team_id,$ feat = 0){



while($ row = mysql_fetch_assoc($ r)){
$ page = explode('_',$ row ['page_id']);
(is_numeric($ page [1]))
? $ pre ='wk_'
:$ pre ='';
$ arr [] = $ page [0]。 |。 $ pre。 $ page [1]。 :。
linked_pa​​th ='.. / news /。 $ page [0]。 /。 $ pre。 $ page [1]。 /。 $ page [1]。 _。 $ page [2]。 .html'
href ='index.php?view = news& yr =。 $ page [0]。 & wk =。 $ page [1]。 & pg =。 $ page [2]。 >< / A>中;
}

$ articles = implode('< br />',$ arr);
返回$ articles;
}

注意 linked_div linked_pa​​th 属性在我的jQuery中使用的锚标记中:

 <$ c $ (函数){
var a = $(this);
a.load()函数set_team_headlines(){
$('#section-articles> a' a.attr('linked_pa​​th')+'#'+ a.attr('linked_div'));
});
}

显然,我不能在PHP中定位锚标签内的HTML标签,因为服务器在加载时没有锚标签内的文本;所以我认为我需要在 load()调用之后剥离jQuery中的HTML ...这是我无法弄清楚如何去做的。 :)



我想要的结果是:

 < a href =...>我的标题< / a> 

我获得的结果是:

 < a href =...>< div id =news_header>我的标题< / div>< / a> ; 

希望这是有道理的。我想我提供了比我需要更多的细节。谢谢!

解决方案

.load 选择器中向下浏览一个级别

  a.load(a.attr('linked_pa​​th')+'#news_header'); 

如果您的news_header id不唯一,则通过该id选择无效)

要解决这个问题,请使用以下命令:

  a.load(a.attr('linked_pa​​th')+'#'+ a.attr('linked_div')+'div'); 

编辑:

.load 实际上包括附加html时的目标元素,而不是附加目标元素的子元素。我会转向使用 $。get()

  $。 get(a.attr('linked_pa​​th'))。done(function(html){
a.text($(html).filter(#news_header)。text());
} );


This is a followup to the solution for this question.

I am using jQuery's load() function to pull a headline within a div tag from one page to another within my site. This works wonderfully.

The problem is, load() also pulls the div tag itself, which I do not want, as it then gets formatted via CSS like the source page.

Here is the PHP:

function get_team_articles($team_id, $feat=0) {
    .
    .
    .
    while ($row = mysql_fetch_assoc($r)) {
        $page = explode('_', $row['page_id']);
        (is_numeric($page[1]))
            ? $pre = 'wk_'
            : $pre = '';
        $arr[] =    $page[0] . " | " . $pre . $page[1] . ": " . "
                    <a linked_div='news_header'
                    linked_path='../news/" . $page[0] . "/" . $pre . $page[1] . "/" . $page[1] . "_" . $page[2] . ".html'
                    href='index.php?view=news&yr=" . $page[0] . "&wk=" . $page[1] . "&pg=" . $page[2] . "'></a>";
    }

    $articles = implode('<br/>', $arr);
    return $articles;
}

Notice the linked_div and linked_path attributes within the anchor tag, which are used in my jQuery:

function set_team_headlines(){
    $('#section-articles > a').each(function() {
        var a = $(this);
        a.load(a.attr('linked_path') + ' #' + a.attr('linked_div'));
    });
}

Obviously I cannot strip the HTML tags within the anchor tags in PHP, because the server doesn't have the text within the anchor tags upon loading; so I assume I need to strip the HTML in jQuery after the load() call...and that is what I cannot figure out how to do. :)

The result I want is:

<a href="...">My headline</a>

The result I'm getting is:

<a href="..."><div id="news_header">My headline</div></a>

Hopefully this makes sense. I think I provided more detail than I needed to. Thanks!

解决方案

Navigate down one more level in your .load selector

a.load(a.attr('linked_path') + ' #news_header');

If your news_header id isn't unique, it isn't valid to select by that id (ID's must be unique!)

To get around that issue, use this:

a.load(a.attr('linked_path') + ' #' + a.attr('linked_div') + ' div');

Edit:
.load actually includes the targeted element when appending html instead of appending the target element's children. I would move to using $.get().

$.get(a.attr('linked_path')).done(function(html) {
    a.text($(html).filter("#news_header").text());
});

这篇关于从jQuery load()结果剥离HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆