我$("#obj").load(url)
之后有没有办法从#obj
获取加载的网址?
答案 0 :(得分:2)
该代码只会使用#obj
调用的结果加载DOM中url
的内容。 jQuery不会将url存储在任何地方。但您可以使用data()
手动存储数据并将其附加到dom对象。像这样:
$("#obj").load(url).data('url', url);
您可以稍后检索这样的网址:
var url = $("#obj").data('url');
有关详细信息,请参阅http://api.jquery.com/data/。
答案 1 :(得分:0)
这是一个实际示例,它使用“语言选择”,并使用Jquery语法加载内容:
体内:
<a href="index.html" class="topnav_estatistics">ESTATISTICS </a>
<a id="idStatistics" href ="#Statistics" > <span lang="en">Statistics </span> <span lang="gr">Στατιστική </span> </a>
<a id="idR_Code_PSPP" href ="#R_Code_PSPP" > <span lang="en">R Code / PSPP </span> <span lang="gr"> R Code / PSPP </span> </a>
<a id="idStat_Article" href ="#Stat_Article" > <span lang="en">Stat Articles </span> <span lang="gr"> Άρθρα </span> </a>
<a id="idPlanets_Health" href ="#Planets_Health" > <span lang="en">Planet's Health </span> <span lang="gr"> Περιβάλλον </span> </a>
<a id="idServices" href ="#Services" > <span lang="en">Services </span> <span lang="gr"> Υπηρεσίες </span> </a>
<!--- The Language options --->
<select id="lang_switch" class="lang_switch">
<option value="en">En </option>
<option value="gr">Gr </option>
</select>
div部分,在该部分中加载了外部html页面div特定内容。
<div id="mainbody" class="main_body"> </div>
<div id="mainbody_2" class="main_body"> </div>
<div id="mainbody_3" class="main_body"> </div>
语言选择以及Jquery文档在当前页面的特定div部分上加载的外部html div特定内容。
<script>
// stackoverflow: 49637061 & 11134701
$('[lang="en"]').show();
$('[lang="gr"]').hide();
$(document).ready(function(){
$("#idR_Code_PSPP").click(function(){
$("#mainbody").load("R_Code_PSPP.html #st_en").data('url_idR_Code_PSPP', "R_Code_PSPP.html#st_en")
$("#left_link_index").load("R_Code_PSPP.html #left_links_en")
var url_idR_Code_PSPP = window.location.protocol+"//"+window.location.hostname+":"+ window.location.port+"/"+ $("#mainbody").data('url_idR_Code_PSPP');
$("#mainbody_2").html(url_idR_Code_PSPP)
});
$("#idStatistics").click(function(){
$("#mainbody").load("Statistics.html #st_en").data('url_idStatistics', "Statistics.html#st_en");
$("#left_link_index").load("Statistics.html #left_links_en")
var url_idStatistics = window.location.protocol+"//"+window.location.hostname+":"+ window.location.port+"/"+$("#mainbody").data('url_idStatistics');
$("#mainbody_2").html(url_idStatistics)
});
$("#idStat_Article").click(function(){
$("#mainbody").load("Stat_Article.html #st_en" ).data('url_idStat_Article', "Stat_Article.html#st_en");
var url_idStat_Article = window.location.protocol+"//"+window.location.hostname+":"+ window.location.port+"/"+$("#mainbody").data('url_idStat_Article');
$("#mainbody_2").html(url_idStat_Article)
});
$("#idPlanets_Health").click(function(){
$("#mainbody").load("Planets_Health.html #st_en").data('url_idPlanets_Health', "Planets_Health.html#st_en");
var url_idPlanets_Health = window.location.protocol+"//"+window.location.hostname+":"+ window.location.port+"/"+$("#mainbody").data('url_idPlanets_Health');
$("#mainbody_2").html(url_idPlanets_Health)
});
$("#idServices").click(function(){
$("#mainbody").load("Services.html #st_en").data('url_idServices', "Services.html#st_en");
$("#left_link_index").load("Services.html #left_links_en");
var url_idServices = window.location.protocol+"//"+window.location.hostname+":"+ window.location.port+"/"+$("#mainbody").data('url_idServices');
$("#mainbody_2").html(url_idServices)
});
$(document).on('click', '#id_bio_history_en', function(){
$("#mainbody").load("Services.html #bio_history_en").data('url_bio_history_en', "Services.html#bio_history_en");
var url_bio_history_en = window.location.protocol+"//"+window.location.hostname+":"+ window.location.port+"/"+$("#mainbody").data('url_bio_history_en');
$("#mainbody_2").html(url_bio_history_en)
});
$(document).on('click', '#id_info_en', function(){
$("#mainbody").load("Services.html #info_en").data('url_info_en', "Services.html#info_en");
var url_info_en = window.location.protocol+"//"+window.location.hostname+":"+ window.location.port+"/"+$("#mainbody").data('url_info_en');
$("#mainbody_2").html(url_info_en)
});
});
$('#lang_switch').change(function () {
var lang = $(this).val();
switch (lang) {
case 'gr':
$('[lang]').hide();
$('[lang="gr"]').show();
$(document).ready(function(){
$("#idR_Code_PSPP").click(function(){
$("#mainbody").load("R_Code_PSPP.html #st_gr").data('url_idR_Code_PSPP', "R_Code_PSPP.html#st_gr");
$("#left_link_index").load("R_Code_PSPP.html #left_links_gr" )
var url_idR_Code_PSPP = window.location.protocol+"//"+window.location.hostname+":"+ window.location.port+"/"+$("#mainbody").data('url_idR_Code_PSPP');
$("#mainbody_2").html(url_idR_Code_PSPP)
});
$("#idStatistics").click(function(){
$("#mainbody").load("Statistics.html #st_gr").data('url_idStatistics', "Statistics.html#st_gr");
$("#left_link_index").load("Statistics.html #left_links_gr")
var url_idStatistics = window.location.protocol+"//"+window.location.hostname+":"+ window.location.port+"/"+$("#mainbody").data('url_idStatistics');
$("#mainbody_2").html(url_idStatistics)
});
$("#idStat_Article").click(function(){
$("#mainbody").load("Stat_Article.html #st_gr" ).data('url_idStat_Article', "Stat_Article.html#st_gr");
var url_idStat_Article = window.location.protocol+"//"+window.location.hostname+":"+ window.location.port+"/"+$("#mainbody").data('url_idStat_Article');
$("#mainbody_2").html(url_idStat_Article)
});
$("#idPlanets_Health").click(function(){
$("#mainbody").load("Planets_Health.html #st_gr").data('url_idPlanets_Health', "Planets_Health.html#st_gr");
var url_idPlanets_Health = window.location.protocol+"//"+window.location.hostname+":"+ window.location.port+"/"+$("#mainbody").data('url_idPlanets_Health');
$("#mainbody_2").html(url_idPlanets_Health)
});
$("#idServices").click(function(){
$("#mainbody").load("Services.html #st_gr").data('url_idServices', "Services.html#st_gr");
$("#left_link_index").load("Services.html #left_links_gr");
var url_idServices = window.location.protocol+"//"+window.location.hostname+":"+ window.location.port+"/"+$("#mainbody").data('url_idServices');
$("#mainbody_2").html(url_idServices)
});
$(document).on('click', '#id_bio_history_gr', function(){
$("#mainbody").load("Services.html #bio_history_gr").data('url_bio_history_gr', "Services.html#bio_history_gr");
var url_bio_history_gr = window.location.protocol+"//"+window.location.hostname+":"+ window.location.port+"/"+ $("#mainbody").data('url_bio_history_gr');
$("#mainbody_2").html(url_bio_history_gr)
});
$(document).on('click', '#id_info_gr', function(){
$("#mainbody").load("Services.html #info_gr").data('url_info_gr', "Services.html#info_gr");
var url_info_gr = window.location.protocol+"//"+window.location.hostname+":"+ window.location.port+"/"+$("#mainbody").data('url_info_gr');
$("#mainbody_2").html(url_info_gr)
});
});
break;
case 'en':
$('[lang]').hide();
$('[lang="en"]').show();
$(document).ready(function(){
$("#idR_Code_PSPP").click(function(){
$("#mainbody").load("R_Code_PSPP.html #st_en").data('url_idR_Code_PSPP', "R_Code_PSPP.html#st_en");
$("#left_link_index").load("R_Code_PSPP.html #left_links_en")
var url_idR_Code_PSPP = window.location.protocol+"//"+window.location.hostname+":"+ window.location.port+"/"+$("#mainbody").data('url_idR_Code_PSPP');
$("#mainbody_2").html(url_idR_Code_PSPP)
});
$("#idStatistics").click(function(){
$("#mainbody").load("Statistics.html #st_en").data('url_idStatistics', "Statistics.html#st_en");
$("#left_link_index").load("Statistics.html #left_links_en")
var url_idStatistics = window.location.protocol+"//"+window.location.hostname+":"+ window.location.port+"/"+$("#mainbody").data('url_idStatistics');
$("#mainbody_2").html(url_idStatistics)
});
$("#idStat_Article").click(function(){
$("#mainbody").load("Stat_Article.html #st_en" ).data('url_idStat_Article', "Stat_Article.html#st_en");
var url_idStat_Article = window.location.protocol+"//"+window.location.hostname+":"+ window.location.port+"/"+$("#mainbody").data('url_idStat_Article');
$("#mainbody_2").html(url_idStat_Article)
});
$("#idPlanets_Health").click(function(){
$("#mainbody").load("Planets_Health.html #st_en").data('url_idPlanets_Health', "Planets_Health.html#st_en");
var url_idPlanets_Health = window.location.protocol+"//"+window.location.hostname+":"+ window.location.port+"/"+$("#mainbody").data('url_idPlanets_Health');
$("#mainbody_2").html(url_idPlanets_Health)
});
$("#idServices").click(function(){
$("#mainbody").load("Services.html #st_en").data('url_idServices', "Services.html#st_en");
$("#left_link_index").load("Services.html #left_links_en");
var url_idServices = window.location.protocol+"//"+window.location.hostname+":"+ window.location.port+"/"+$("#mainbody").data('url_idServices');
$("#mainbody_2").html(url_idServices)
});
$(document).on('click', '#id_bio_history_en', function(){
$("#mainbody").load("Services.html #bio_history_en").data('url_bio_history_en', "Services.html#bio_history_en");
var url_bio_history_en = window.location.protocol+"//"+window.location.hostname+":"+ window.location.port+"/"+$("#mainbody").data('url_bio_history_en');
$("#mainbody_2").html(url_bio_history_en)
});
$(document).on('click', '#id_info_en', function(){
$("#mainbody").load("Services.html #info_en").data('url_info_en', "Services.html#info_en");
var url_info_en = window.location.protocol+"//"+window.location.hostname+":"+ window.location.port+"/"+$("#mainbody").data('url_info_en');
$("#mainbody_2").html(url_info_en)
});
});
break;
};
});
</script>
外部HTML文件必须包含一个名为例如
的div部分。 <div id="info_gr" class="info_services" > and/or
<div id="info_en" class="info_services" > or
<div id="bio_history_en" class="biohistory" > and/or
<div id="bio_history_gr" class="biohistory" >
脚本恰好在结尾之前,特别是在"</html>".
之前