0

www.sitetwo.comから呼び出されたいくつかのスクリプトを使用してフォームを生成していwww.siteone.comます。にフォームを送信する必要があるためwww.sitetwo.comwww.siteone.comJSONP メソッドを使用して Ajax を使用しています。ただし、生成されたフォームはスクリプトをサポートしていません。私のコードはここにあります:

   //script in www.sitetwo.com
    <script type="text/javascript">
    window.onload = function () {
        "use strict";
        function js(n) {
            var s = document.createElement('script');
            s.setAttribute("type", "text/javascript");
            s.setAttribute("src", n);
            document.getElementsByTagName("head")[0].appendChild(s);
        }
        js("http://www.siteone.com/script.js");
    };
    $(document).ready(function(){
    $=jQuery.noConflict();
    $(".submitbutton").hover(function(){
      alert("hai");
    }
    </script>
    <div id="newform">
    </div>

id newform の div で動的にレンダリングされるフォームは次のとおりです。

<form>
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="go" class="submitbutton">
</form>

ホバーアラートの基本的なスクリプトを適用しようとしましたが、うまくいきません。動的に生成されたフォームに、このようなスクリプトを直接使用することはできません。もしそうなら、どうすればこの問題を克服できるでしょうか。ブラウザでエラーを検索しました。エラーは表示されません。解決策は何ですか??

4

3 に答える 3

2

You can't use hover in that case, need to use delegated event handlers using events mouseenter and mouseleave

.hover() is not a event, it is a helper method to register mouseenter and mouseleave event handlers

Bind two handlers to the matched elements, to be executed when the mouse pointer enters and leaves the elements.

$(document).on('mouseenter', '.submitbutton', function(){
  alert("hai");
}
于 2013-08-22T12:49:42.190 に答える