尝试将链接/href 路径附加到原始代码
我正在将网站不同页面的内容加载到结果容器(div 类)中。
$('.result-container').load('../path/otherpage.html #ID');
到目前为止一切顺利,有效。 现在,我需要在单击结果容器中的任何链接时更新它。原因是,它们所有的链接仍然指向它们的原始位置(从它们加载的位置)。
这个想法是,当单击链接时,我得到 href 属性。如果它以“files/”开头,则它是相对于原始页面的链接(如果它以其他内容开头,则它是到其他地方的链接,这是可以的)。因此,我需要添加到(刚刚单击的)href“../path/”的前面,以使最终的href路径看起来像“../path/files/etc....”。 我有一些工作,但真的很难将它们拼凑在一起......有人可以帮我吗? 谢谢! M.
$(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 容器加载内容后,知道如何执行并显示更改后的内容吗?
I am loading content from a different page of my site into a result-container (div class).
$('.result-container').load('../path/otherpage.html #ID');
So far so good, that works.
Now, I need to update any link within the result-container when it s clicked. Reason being, they all links still point to their original location (from where they were loaded from).
The idea is , when clicking on a link, I get the href attribute. If it starts with "files/", than it is a link relative to the original page (if it starts with something else, it is a link to somewhere else and is ok). So, I need to add to the front of the (just clicked) href "../path/", to make the final href path look like "../path/files/etc....".
I have some bits working, but really struggle to piece it all together.... can anyone reach me a hand please?
Thanks!
M.
$(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
};
}
)};
)};
Excellent, Thank you !!!!!
Now I am loading an element from a different page on my site with:
$('.result-container').load(../path/conetent.html #unique-ID); //load conent from different page on my site
on click, it changes every element which starts with "files/" to "../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);
}); });
That works. This changes the original content and displays the altered content after an active click in the .result-container.
Now I am trying to remove the .live("click", trigger here. (This trigger is only there, because my initial approach to achieve this was different). But when I remove the "click", this script does not change the text automatically... Any idea on how this executes and displays the altered content after I load the content from the unique ID container?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将字符串“files/”的任何实例替换为“/path/”怎么样?所以,
如果我的正则表达式有点不对,你必须原谅我,但这应该会给你一个想法。
How about just replacing any instance of the string "files/ with "/path/. So something like
You'll have to forgive me if my regex is a bit off but that should give you an idea.