0

我们正常使用http://jscolor.com(JavaScript 颜色选择器)没有任何问题。但是当我们通过 AJAX 创建输入元素时,它无法正常工作,并且 jscolor.js 无法检测输入的类别(颜色),而输入显示正确。我们应该怎么做?

HTML 代码是:

<html>
<head>
<script src='/js/jscloro.js'></script>
<script>
function showHint(str)
{
if (str.length==0)
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","gethint.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<a href="#" onClick="showHint('jscolorpicker')">FORM</a>

<div id="txtHint"></div>

</body>
</html>

我们对 ajax 的 PHP 响应是:echo "<input class=\"color\" type=\"text\" value=\"66ff00\">";

4

1 回答 1

1

我认为,当您在加载DOM后创建新元素时document,您应该在创建该元素的位置之后绑定事件。

答案的更新部分 请参阅此 html 和脚本:

<div id="container"></div>

$(document).ready(function () {
        // if you add the event for element that currenlty not exist on
        // page, and later may be created, the even cannot fired
        $('#elem').click(function () {
            alert('You are clicked on input!');
        });

        $.ajax({
            url: 'somePage.aspx',
            type: 'POST',
            contentType: 'application;json/ charset=utf-8',
            dataType: 'json',
            data: {},
            success: function (msg) {

                // if you create your own element here
                $('#container').append(function () {
                    return $('<span>')
                        .text('This Is New Element')
                        .attr('id', '#elem');
                });
            }
        });
    });

但正确的方法是event在创建元素之后添加DOM,如下所示:

    $(document).ready(function () {
        $.ajax({
            url: 'somePage.aspx',
            type: 'POST',
            contentType: 'application;json/ charset=utf-8',
            dataType: 'json',
            data: {},
            success: function (msg) {

                // if you create your own element here
                $('#container').append(function () {
                    return $('<span>')
                        .text('This Is New Element')
                        .attr('id', '#elem')
                        .click(function () { // you should bind your events here
                            alert('You are clicked on input!');
                        });
                });
            }
        });
    });

更新第 2 部分

您应该初始化新的 jscolor 实例,例如使用此代码

new jscolor.color($('.color'), {});

在您创建自己的元素之后。

更新第 3 部分

<html>
<head>

    <script src='/js/jscloro.js'></script>
    <script>
        function showHint(str) {
            if (str.length == 0) {
                document.getElementById("txtHint").innerHTML = "";
                return;
            }
            if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            }
            else {// code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    document.getElementById("txtHint").innerHTML = xmlhttp.responseText;

                    /* YOU SHOULD INITIALIZE THE NEW JSCOLOR INSTANCE HERE */
                     var myPicker = new jscolor.color(document.getElementById('myField1'), {})
                     myPicker.fromString('99FF33') // 
                    /**/
                }
            }
            xmlhttp.open("GET", "gethint.php?q=" + str, true);
            xmlhttp.send();
        }
    </script>
</head>
<body>
    <a href="#" onclick="showHint('jscolorpicker')">FORM</a>

    <div id="txtHint"></div>

</body>
</html>

如果对您有帮助,请将其标记为答案。

于 2013-10-28T18:34:03.050 回答