传递html块作为参数

时间:2013-07-19 07:20:59

标签: javascript jquery jquery-mobile

我知道这是一个非常简单的问题,我想知道如何将html块作为参数传递给函数,并在页面加载时调用该函数。

<body>
<section data-role="page" data-theme="ams">
    <section data-role="header" id="content">
        <h1>header ssssssssssssssssssssssssssssss sssssssssssssssssssssss ssssssssssssssssssssssssss ssssssssssssssss ssssssssssss</h1>

    </section>
    <section data-role="conents" >
        <p>loruim epsum haha&nbsp;</p>\n<p>hehe told u&nbsp;</p>\n<ol class=\"starpasspro-upper-alpha_ol\">\n<li>gasdfg</li>\n<li>sdfffasd</li>\n<li>asdfffasdf</li>\n<li>asdfasdfasdfasd</li>\n</ol>\n\n<table class=\"table_with_header lsm-tableclass-styles\">\n<tbody>\n<tr>\n<td class=\"lsm_table_heading_1\">Description</td>\n<td class=\"lsm_table_heading_1\">Amount</td>\n</tr>\n<tr>\n<td>Gross salary</td>\n<td>&nbsp;xxxx</td>\n</tr>\n<tr>\n<td>Less: income tax (£35,000 x 25%)</td>\n<td>&nbsp;xxxx</td>\n</tr>\n<tr>\n<td>Less: social security tax (£50,000 x 9%)</td>\n<td>&nbsp;xxxx</td>\n</tr>\n<tr>\n<td>Net earnings</td>\n<td>&nbsp;xxxx</td>\n</tr>\n<tr>\n<td>Employer’s contribution on social security<br>(£50,000 x10.5%)</td>\n<td>&nbsp;xxxx</td>\n</tr>\n</tbody>\n</table>\n\n
        <p><span data-lscp-resource-mimetype=\"image/png\" data-lscp-resource-id=\"51e8db617607942807000032\"></span></p>


    </section>
    <section data-role="footer">
        <p>footer</p>

    </section>
</section>

我想将以下html块作为参数传递给我的函数

  <p>loruim epsum haha&nbsp;</p>\n<p>hehe told u&nbsp;</p>\n<ol class=\"starpasspro-upper-alpha_ol\">\n<li>gasdfg</li>\n<li>sdfffasd</li>\n<li>asdfffasdf</li>\n<li>asdfasdfasdfasd</li>\n</ol>\n\n<table class=\"table_with_header lsm-tableclass-styles\">\n<tbody>\n<tr>\n<td class=\"lsm_table_heading_1\">Description</td>\n<td class=\"lsm_table_heading_1\">Amount</td>\n</tr>\n<tr>\n<td>Gross salary</td>\n<td>&nbsp;xxxx</td>\n</tr>\n<tr>\n<td>Less: income tax (£35,000 x 25%)</td>\n<td>&nbsp;xxxx</td>\n</tr>\n<tr>\n<td>Less: social security tax (£50,000 x 9%)</td>\n<td>&nbsp;xxxx</td>\n</tr>\n<tr>\n<td>Net earnings</td>\n<td>&nbsp;xxxx</td>\n</tr>\n<tr>\n<td>Employer’s contribution on social security<br>(£50,000 x10.5%)</td>\n<td>&nbsp;xxxx</td>\n</tr>\n</tbody>\n</table>\n\n
        <p><span data-lscp-resource-mimetype=\"image/png\" data-lscp-resource-id=\"51e8db617607942807000032\"></span></p>

任何想法怎么做?

请注意,这有jquery移动结构

3 个答案:

答案 0 :(得分:1)

function yourFunction(htmlBlock){
  // your code
}

// call function and pass Jquery object
yourFunction($('section[data-role="conents"]'));
// call function and pass html code of object
yourFunction($('section[data-role="conents"]').html());

答案 1 :(得分:0)

因为你正在使用jquery,因为你可以获得html

var inputHtml = $("section[data-role='conents']").html();

只需将inputHtml传递给任何函数

答案 2 :(得分:0)

当您使用jQuery mobile时,您可以尝试使用

var htmlBlock = $('section[data-role="conents"]').html();

yourFunction(htmlBlock);