English 中文(简体)
Jquery - 使用. 附录时停止自动填写 HTML 标记
原标题:Jquery - Stop auto complete of HTML tags when using .append
  • 时间:2012-05-23 12:29:13
  •  标签:
  • jquery

我试图从 XML 创建一个嵌套列表, 然而我发现... 附录函数似乎自动完成我的 HTML 标记, 有用, 已商定, 但如何在它不被通缉 OR 有更聪明的方法做下列操作时, 如何覆盖此操作 :

$( #name ).html( <li> + Origin + <ul> +    ).html();
$(this).find("origin").each (function() {
var name =  <li>origin:   + $(this).text() +  </li> ;
$( #name ).append(name);
$( #name ).append( </ul></li> );

我得到的是:

<ul id="name">
<li>
Origin
<ul> </ul>
</li>
<li>origin: network</li>
<li>origin: nevil-nmshub</li>
<li>origin: origin_overide</li>
<li>origin: Support_4</li>
<li>origin: test-origin</li>
</ul>

正如你可以看到它添加标签之前,我想要它们。我想也许我错了,有什么建议吗?

问题回答

. append () (与 .innerHTML 一样) 只能插入 entire DOM 元素 ,而不是任意的 HTML 不完整片段。

因此,根据定义,您第一次 . 附加() something,其中的任何标签都将隐含封闭。

试试这个代替:

var $li = $( <li> ).appendTo( #name );
var $ul = $( <ul> ).appendTo($li);
$(this).find("origin").each(function() {
    $( <li> , { text:  origin:   + $(this).text() }).appendTo($ul);
});

将代码更改为 :

var ul = $( <ul /> );

// I believe you mean to find elements with class origin instead of <origin>
$(this).find(".origin").each (function() {
   var name =  <li>origin:   + $(this).text() +  </li> ;
   ul.append(name);
});
$( #name ).append(ul);

当您正在动态创建 HTML 时, 尽量避免将打开和关闭标记分隔开口和关闭标记 。

你可以做到这一点:

$( #name ).append( <li>Origin<ul></ul></li> );

// I think  origin  is an XML tag.
// So no worries, you can call in like an HTML element
$(this).find( origin ).each(function(index, value) {
    $( #name ul ).append( <li>origin:   + value +  </li> )
});




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签