Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquerymobile与自动完成冲突:$this.attr(";href";)未定义_Jquery_Jquery Autocomplete_Jquery Mobile - Fatal编程技术网

jquerymobile与自动完成冲突:$this.attr(";href";)未定义

jquerymobile与自动完成冲突:$this.attr(";href";)未定义,jquery,jquery-autocomplete,jquery-mobile,Jquery,Jquery Autocomplete,Jquery Mobile,当我在jquery mobile alpha 2上使用jquery ui自动完成1.8.5版时。 单击“自动完成”列表中的项目时出错: $this.attr(“href”)未定义 有人知道有什么办法吗 编辑: <html> <head> <link rel="stylesheet" type="text/css" href="css/ui-lightness/jquery-ui-1.8.custom.css"> <

当我在jquery mobile alpha 2上使用jquery ui自动完成1.8.5版时。 单击“自动完成”列表中的项目时出错:

$this.attr(“href”)未定义

有人知道有什么办法吗

编辑:

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="css/ui-lightness/jquery-ui-1.8.custom.css">
        <link rel="stylesheet" type="text/css" href="css/autocomplete.css">
    </head>
    <body>

        <div id="formWrap">
            <form id="messageForm" action="#">
                <fieldset>
                    <label id="toLabel">select:</label>
                    <div id="friends" class="ui-helper-clearfix">

                        <input id="to" type="text">
                    </div>

                </fieldset>
            </form>
        </div>
        <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
        <script type="text/javascript" src="js/jquery.mobile-1.0a2.js"></script>
        <script type="text/javascript" src="js/jquery-ui-1.8.custom.min.js"></script>


        <script type="text/javascript">
            $(function(){

                var availableTags = [
                        "ActionScript",
                        "AppleScript",
                        "Asp",
                        "BASIC",
                        "C",
                        "C++",
                        "Clojure",
                        "COBOL",
                        "ColdFusion",
                        "Erlang",
                        "Fortran",
                        "Groovy",
                        "Haskell",
                        "Java",
                        "JavaScript",
                        "Lisp",
                        "Perl",
                        "PHP",
                        "Python",
                        "Ruby",
                        "Scala",
                        "Scheme"
                    ];


                //attach autocomplete
                $("#to").autocomplete({             

                    source:availableTags
                    ,

                    //define select handler
                    select: function(e, ui) {
                        var contact = ui.item.value;
                        createSpan(contact);
                        $("#to").val("").css("top", 2);
                        return false;
                    }           
                });

                });

            function createSpan(contact){
                //create formatted friend               
                span = $("<span>").text(contact)            
                //add contact to contact div
                span.insertBefore("#to");
            }
        </script>

    </body>
</html>

选择:
$(函数(){
var availableTags=[
“动作脚本”,
“AppleScript”,
“Asp”,
“基本”,
“C”,
“C++”,
“Clojure”,
“COBOL”,
“ColdFusion”,
“二郎”,
“Fortran”,
“好极了”,
“哈斯克尔”,
“爪哇”,
“JavaScript”,
“口齿不清”,
“Perl”,
“PHP”,
“Python”,
“红宝石”,
“斯卡拉”,
“方案”
];
//附加自动完成
$(“#to”).autocomplete({
资料来源:availableTags
,
//定义选择处理程序
选择:功能(e、ui){
var contact=ui.item.value;
createSpan(联系人);
$(“#至”).val(“”.css(“顶部”,2);
返回false;
}           
});
});
函数createSpan(联系人){
//创建格式化的朋友
span=$(“”)。文本(联系人)
//将联系人添加到联系人div
span.插入前(“#至”);
}

$(this)
不是
$this
修改了jquery.mobile-1.0a2.js:添加了一个检查以查看href是否未定义 代码内部

  $( "a" ).live( "click", function(event) { 
           //( START: My added code) 
            if($(this).attr( "href" )==undefined){
                //for links created for interaction - ignore
                return false;
            }
           //( END: My added code) 
   //Remaining code follows

这解决了问题。

嘿,甜心,谢谢你给我指明了正确的方向

我发现最好覆盖jqueryui代码,而不是直接修改jquerymobile源代码(为了可维护性)。以下内容覆盖呈现自动完成列表中每个项目的jquery ui方法;它将值为“#”的href属性添加到正在创建的锚元素中。这将防止出现未定义的href错误:

$('#to').data( "autocomplete" )._renderItem = function( ul, item ) {
return $( "<li></li>" )
    .data( "item.autocomplete", item )
    .append( $( "<a></a>" ).attr({href: '#'}).html( item.label ) )
    .appendTo( ul );
$('to')。数据(“自动完成”)。\u renderItem=函数(ul,项目){
返回$(“
  • ”) .data(“item.autocomplete”,item) .append($(“”).attr({href:'#'}).html(item.label)) .附录(ul);

    }

    链接到显示该事件发生的页面会很有帮助。由于我在本地工作,因此无法发送url。代码很长。示例可以是任何简单的jqueryautocomplete,其中包括“jquery.mobile-1.0a2.js”。jquery.mobile-1.0a2.js中的var$this=$(this)。