将网站内容加载到属性中 [英] Load content from website into an attribute

查看:96
本文介绍了将网站内容加载到属性中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用URL来更新链接(#product_buy)的href,该URL可以在#product_buy_source div内的另一页上找到.这是jQuery.

I need to update the href of a link (#product_buy) with a URL that can be found on another page inside the #product_buy_source div. Here's the jQuery.

$('body').ready(function(){

    $('#product_buy').attr('href', function(){
        $(this).load('http://cdn.jeremyblaze.com/theme_info.html #Altitude .product_buy_source');
    });

});

这是需要编辑的链接.

<a href="#" id="product_buy">Purchase</a>

我感觉到我使用的jQuery是完全错误的.有人能使它工作吗?这是一个小提琴

I sense the jQuery I've used is completely wrong. Is anybody able to make it work? Here's a little fiddle

推荐答案

.load()将数据作为调用元素的HTML加载.要使用.load()(以便您可以同时指定一个选择器)来设置href,则需要输入类似以下内容:

.load() loads data as the HTML of the element it is called on. To use .load() (so that you could specify a selector along) for setting the href, you will need to to something like:

// load it into a temp element, and assign the href once it is loaded
$('<div/>').load('http://cdn.jeremyblaze.com/theme_info.html #Altitude .product_buy_source', function() {
    $('#product_buy').attr('href', $(this).text());
});

或者,以更直接的方式,例如:

Or, in a more straightforward manner like:

$.get('http://cdn.jeremyblaze.com/theme_info.html', function(data) {
    $('#product_buy').attr('href', $(data).find('#Altitude .product_buy_source').text());
});

这篇关于将网站内容加载到属性中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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