jquerymobile冲突与自动完成:$这一点。attr("href")是不确定
-
09-10-2019 - |
题
当我使用便捷的自动完成的版本1.8.5字移动阿尔法2.我得到一个错误,当我击中的项目自动列表:
$这一点。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>
解决方案
修改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
这个固定的问题。
其他提示
嘿,亲爱的,谢谢你为我指明了正确的方向。
我发现最好重写 jquery ui 代码,而不是直接修改 jquery mobile 源代码(为了可维护性)。以下重写了呈现自动完成列表中每个项目的 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 );
}
$(this)
不是 $this
.
不隶属于 StackOverflow