将新元素添加到现有的Jquery&使用Javascript

时间:2011-03-16 11:20:21

标签: javascript jquery

我正在尝试向现有DOM元素添加一些元素但是失败了。

这是我试过的

方法第一

$('#b-friend-selector').append('<script type="text/fbml">
   <fb:fbml>
      <fb:request-form
           action="http://<?php echo $_SERVER[\'HTTP_HOST\']?>/"
           target="_top"
           method="POST"
           invite="true"
           type="test"
           align="left"
           content="I am a member of <fb:req-choice url=\'http://www.facebook.com/apps/application.php?id=xxxxxx/requests\' label=\'Accept\' />" >       
           <fb:multi-friend-selector
        showborder="false"
        actiontext="Select your friends and invite them"
        eail_invite="false"
        cols="4"
        max="32"
        import_external_friends="false">
         </fb:request-form>
        </fb:fbml>
    </script> '/*It is echoing next contents blindly*/ );

它在我的网页上回显);

如果我从此字符串中删除<script>标记,则会在浏览器控制台unterminated string中显示错误。

此外,我尝试将此字符串放在单行中没有js错误,并且仍在我的网页上打印);。 第二个用javascript

 var script = document.createElement( 'script' );
 script.setAttribute('type',"text/html");
 var fbml=document.createElement('fb:fbml');
 var requestForm=document.createElement('fb:request-form');
 requestForm.setAttribute('action',"http://<?php echo $_SERVER['HTTP_HOST'];?>");
 requestForm.setAttribute('target',"_top");
 requestForm.setAttribute('method',"post");
 requestForm.setAttribute('invite',"true");
 requestForm.setAttribute('type',"Test");
 requestForm.setAttribute('align',"left");
 requestForm.setAttribute('content',"testmes<fb:req-choice url='http://www.facebook.com/apps/application.php?id=xxxxxxx/requests' label='Accept' />");
 var multiSelect=document.createElement('fb:multi-friend-selector');

 requestForm.setAttribute('showborder',"false");
 requestForm.setAttribute('actiontext',"Select your friends and invite them");
 requestForm.setAttribute('email_invite',"false");
 requestForm.setAttribute('cols',"4");
 requestForm.setAttribute('max',"32");
 requestForm.setAttribute('import_external_friends',"false");
 var main= document.getElementById('b-friend-selector');
 main.appendChild( script );        
 script.appendChild( fbml );
 fbml.appendChild( requestForm );
 requestForm.appendChild( multiSelect );

你能建议一种有效的方法吗?

1 个答案:

答案 0 :(得分:0)

多行字符串应以\

结尾

你可以尝试这个,但可能问题是你不应该逃避它,如果你的文件没有被php解析(例如.js或.html)

$('#b-friend-selector').append('<script type="text/fbml"></script>').html(
   '<fb:fbml>\
      <fb:request-form\
           action="http://<?php echo $_SERVER[\'HTTP_HOST\']?>/"\
           target="_top"\
           method="POST"\
           invite="true"\
           type="test"\
           align="left"\
           content="I am a member of <fb:req-choice url=\'http://www.facebook.com/apps/application.php?id=xxxxxx/requests\' label=\'Accept\' />" >       \
           <fb:multi-friend-selector\
        showborder="false"\
        actiontext="Select your friends and invite them"\
        eail_invite="false"\
        cols="4"\
        max="32"\
        import_external_friends="false">\
         </fb:request-form>\
        </fb:fbml>'
    );