尝试将link / href路径附加到原始代码

时间:2011-05-19 21:00:50

标签: jquery if-statement href

我正在将我网站的不同页面中的内容加载到结果容器(div类)中。

    $('.result-container').load('../path/otherpage.html #ID');

到目前为止,这很好,有效。 现在,我需要在点击时更新结果容器中的任何链接。原因是,它们的所有链接仍然指向它们的原始位置(从它们加载的位置)。

这个想法是,当点击一个链接时,我得到了href属性。如果它以“files /”开头,那么它是相对于原始页面的链接(如果它以其他东西开头,则它是指向其他地方的链接并且没问题)。所以,我需要添加到(只是单击)href“../path/”的前面,使最终的href路径看起来像“../ path / files / etc ....”。 我有一些工作,但真的很难把它拼凑在一起....任何人都可以伸手帮我吗? 谢谢! 微米。

    $(function() {
    $(".result-container").live("click",   //on click on element
                     function() {
                     $.get($(this).attr("href"), //get the href attribute                                              
                              if ($('a[href^="files/"]'))   //if attribute starts with "files/"
                                 {function() {
                                 var href = $(this).attr('href'); //get the attribute content - guess I could have done that above?
                                 $(this).attr('href', '../start/path/' + href); //append the start path to original path
                                             };
                                 }
                           )};
                           )};

很好,谢谢!!!!! 现在我正在使用以下网址从我网站上的其他页面加载元素:

$('.result-container').load(../path/conetent.html #unique-ID); //load conent from different page on my site
单击

,它将以“files /”开头的每个元素更改为“../ path / files /”:

  $(function() {
    $(".result-container").live("click",
                     function() {
                        $('.result-container').html();
                        var test = $('.result-container').html().replace(/"files\//gi, '"../path/files/');
                        $('.result-container').html(test);                       
                           }); }); 

有效。这会更改原始内容,并在.result-container中激活单击后显示更改的内容。 现在我试图删除.live(“click”,触发器在这里。(这个触发器只在那里,因为我的初始方法实现这一点是不同的。)但是当我删除“click”时,这个脚本不会改变文本自动...在我从唯一ID容器加载内容后,如何执行并显示更改的内容?

1 个答案:

答案 0 :(得分:1)

如何更换字符串“files / with”/ path /的任何实例。像

这样的东西
var new_text = $('#my_div').html().replace(/"files\//gi, '"/path/');
$('#my_div').html(new_text);

如果我的正则表达式稍微偏离,你将不得不原谅我,但这应该会给你一个想法。