jQuery只加载某些元素

时间:2012-08-06 12:16:52

标签: jquery load element

使用PHP在列表中创建一个新的href。如何使用jQuery我们只能加载新的href而不是整个容器。

<div class="container">
<a href="" id="test1">Test 1</a>
<a href="" id="test2">Test 2</a>
<a href="" id="test3">Test 3</a>
<a href="" id="test4">Test 4</a>
</div>

现在我们知道我们用PHP创建了一个元素#test5,我们如何在这个列表的末尾加载它。

5 个答案:

答案 0 :(得分:0)

如果您有代码,请使用

$('.container').append('<a href="" id="test5">Test 5</a>');

如果需要从PHP脚本加载数据,请使用以下代码:

$.get('http://example.com', null, 
   function(data){$('.container').append(data);}
);

答案 1 :(得分:0)

这应该这样做。

$('div.container').append('<a href="" id="test5">Test 5</a>'); 

答案 2 :(得分:0)

您可以使用appendappendTo

$('<a href="" id="test5">Test 5</a>').appendTo('.container');

如果您从ajax请求收到完整的锚标记(例如:<a href="soemthing.php">Test5</a&gt;),则只需附加该标记即可。

$.get("fromsomefile.php",function(data){
   $(data).appendTo('.container');
});

如果您获得了href(Ex: ="sompage.php")值,则创建一个链接并设置href属性值

$.get("fromsomefile.php",function(data){
   $('<a id="someDynamcId">Tes 5</a>').attr("href",data).appendTo('.container');
});

答案 3 :(得分:0)

如果我没有提出错误的问题,你只需加载来自php的标签......我会在代码上显示...让我们说

 $.post("some.php",{some:parameter},function(data){
    //data is <a href="" ></a>
    $(".container").append(data);
 });

答案 4 :(得分:-1)

如果元素已经加载,请使用:

$('#test4').attr('href','whatever you want to change');
相关问题