2

以下のコードは正常に機能しています。しかし、newPara()のhtml文字列をに変更する<textarea></textarea><button id="someId">Save</button><textarea></textarea><input type="button" value="Save"/>、機能しない場合。これの問題は何ですか?

新しく添付されたhtmlを削除するには、そのid属性が必要です。

助言がありますか!

ありがとう!

<html>
  <head>
    <style>
      p { background:yellow; font-weight:bold; cursor:pointer; padding:5px; }
      p.over { background: #ccc; }
      span { color:red; }
    </style>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
  </head>
  <body>
    <div id="aDiv">
      <p>Click me!</p>
    </div>

    <span></span>
    <script>
      $("body").delegate("p", "click", newPara);
      $("body").undelegate("button", "click", newPara).find("#adiv").html("<p>Click me!</p>");
      function newPara() {
        var html = "<textarea></textarea><button>Save</button>";
        $(this).after(html);
      }
    </script>
  </body>
</html>
4

2 に答える 2

1

後ではなく追加をお探しですか?またはhttp://api.jquery.com/insertAfter/

于 2010-11-01T12:57:40.923 に答える
1

My only guess would be that you're using double quotes to create the string literal, but also using them inside the string, therefore terminating it early.

If so, this:

"<textarea></textarea><button id="someId">Save</button>"

should be:

'<textarea></textarea><button id="someId">Save</button>'

Notice that I used single quotes on the outside, so that the inside ones don't close the string.

Try it out: http://jsfiddle.net/QQrHr/

The example uses your exact code. The only change I made was to use singe quotes around the string.

You could also escape the inner quotes if you wanted:

"<textarea></textarea><button id=\"someId\">Save</button>"
于 2010-11-01T12:57:59.917 に答える