0

I have the HTML in my DOM, you can find the HTML code below. I would like to write the javascript function which return me image and font tags by id. How can I do this?

<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>

How can I write the function which return me img and font tags by id?

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>'
}

Thanks!

4

2 回答 2

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
    };
}
于 2013-04-26T09:11:44.230 回答
0

鉴于 id 是输入参数

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

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

return obj = {
  "img" : img,
  "font" : font
};
于 2013-04-26T09:08:36.393 回答