2013-04-26 68 views
0

我在我的DOM中有HTML,你可以在下面找到HTML代码。我想写一个javascript函数,它通过id返回我的图像和字体标签。我怎样才能做到这一点?jQuery过滤DOM结构

<ul id="ChatUsers"> 
    <li class="ajaxchat_Available" id="ajaxchat_42"> 
     <img src="http://cash.co/modules/mod_ajaxchat/images/countryflags/us.gif"> 
     <strong> 
      <a onclick="javascript:chatWith(42, 'us',) 'darkgreen')" href="javascript:void(0)"> 
       <font color="darkgreen"><b>&nbsp;&nbsp;Admin</b></font> 
      </a> 
     </strong> 
    </li> 
    <li class="ajaxchat_Available" id="ajaxchat_48" 
     <img src="http://cash.co/modules/mod_ajaxchat/images/countryflags/xx.gif"> 
     <strong> 
      <a onclick="javascript:chatWith(48, 'xx', '#ff0000')" href="javascript:void(0)"> 
       <font color="#ff0000">&nbsp;&nbsp;tony</font> 
      </a> 
     </strong> 
    </li> 
</ul> 

我该如何编写这个函数,它通过id返回我的img和font标签?

function get_data_by_id(42) { 
... 
jQuery filter code here... 

return { 
    img: '<img src="http://cash.co/modules/mod_ajaxchat/images/countryflags/us.gif">', 
    font: '<font color="darkgreen"><b>&nbsp;&nbsp;Admin</b></font>' 
} 

谢谢!

+1

那么你尝试过什么? – Sirko 2013-04-26 09:01:53

+0

为什么你需要一个功能?你所要做的只是阅读文档,然后找出'$('#ajaxchat_48')'就足够了。 – adeneo 2013-04-26 09:03:03

回答

1

试试这个:

function get_data_by_id(id) { 
    var img = $("#ajaxchat_" + id).find("img").get(0); 
    var font = $("#ajaxchat_" + id).find("font").get(0); 

    return obj = { 
     "img" : img, 
     "font" : font 
    }; 
} 
0

因为id是输入参数

var img = $("#" + id).find("img").outerHTML(); 

var font = $("#" + id).find("Strong a font").outerHTML(); 

return obj = { 
    "img" : img, 
    "font" : font 
};