I managed to add a link to the right of the jQuery autocomplete items, using the following code:
function onClientSelect(event, ui) {
// My handling code goes here
event.preventDefault();
}
$("#mypage input[name='client']").autocomplete({
minLength: 1,
appendTo: $(this).parent(),
source: '/filter_client/',
select: onClientSelect,
focus: function (event, ui) {
event.preventDefault();
},
}).data("ui-autocomplete")._renderItem = function (ul, item) {
return $("<li></li>")
.data("item.autocomplete", item)
.append("<a>" + item.label + "</a>")
.appendTo(ul);
};
My server returns element label in this format:
<div class="ac-item-lb">My Item Label</div>
<a class="ac-item-a" href="/url_to_go_to/" target="_blank">View Detail</a>
What I want to do is to open the link in a new tab when I click on 'View Detail', which clicking any other area executes onClientSelect just like normal. However, currently left-clicking on the link also executes onClientSelect
. The only way to open the link is to click the middle wheel or right mouse button and select 'open in new tab'.
I tried attaching a click event handler on the link with event.stopImmediatePropagation() but that doesn't seem to work. Does anyone have any idea how to fix this?
答案 0 :(得分:21)
一种方法是定义自定义select
功能,而不使用<a>
作为链接
HTML:
<div class="ui-widget">
<label for="tags">Tags:</label>
<input id="tags">
</div>
JS:
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"];
$("#tags").autocomplete({
source: availableTags,
select: function (event, ui) {
var elem = $(event.originalEvent.toElement);
if (elem.hasClass('ac-item-a')) {
var url = elem.attr('data-url');
event.preventDefault();
window.open(url, '_blank ');
}
}
}).data("ui-autocomplete")._renderItem = function (ul, item) {
return $("<li></li>")
.data("item.autocomplete", item)
.append('<a>' + item.label + '<span class="ac-item-a" data-url="http://www.nba.com"> View Details</span></a>')
.appendTo(ul);
};
答案 1 :(得分:5)
您是否考虑过将点击处理程序中的条件设置为不 preventDefault
?如果您测试元素是否应该正常运行,那么您可以在return
之前preventDefault
,然后链接应该正常运行。
以下是一个示例(JSBin):
function clickHandler(e) {
// If the target element has the class `test-clickable` then the
// function will return early, before `preventDefault`.
if (this.className.indexOf('test-clickable') !== -1) {
return;
}
alert('That link is not clickable because of `preventDefault`');
e.preventDefault();
}
$('a').click(clickHandler);
&#13;
a {
display: block;
}
a:after {
content: ' [class=' attr(class)']';
}
&#13;
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Title</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<a class='test-clickable' href='http://www.google.com' target='_blank'>Link</a>
<a class='test-not-clickable' href='http://www.google.com' target='_blank'>Link</a>
</body>
</html>
&#13;
答案 2 :(得分:3)
Amir,关于你的回答,我认为popupBlocker可以阻止来自 select 事件的javascript函数内的打开窗口
pinghsien422,你的服务器端应该返回Json并且不能完成html,如果必须返回完整的html,请进行以下更改:1.删除clientSelect功能
2.No&#39; a&#39; renderData函数
中需要tag $("#mypage input[name='client']").autocomplete({
minLength: 1,
appendTo: $(this).parent(),
source: '/filter_client/',
focus: function (event, ui) {
event.preventDefault();
},
}).data("ui-autocomplete")._renderItem = function (ul, item) {
return $("<li></li>")
.data("item.autocomplete", item)
.append( item.label )
.appendTo(ul);
};
答案 3 :(得分:3)
好的,jquery的事件优先于你的选择事件 - 无论你做什么,你都无法阻止任何事情。但您可以代理menuselect事件,然后决定是否应该调用它(jsfiddle)。
var ac = $("#mypage input[name='client']").autocomplete({
minLength: 1,
appendTo: document.body,
source: availableTags,
focus: function (event, ui) {
event.preventDefault();
},
}).data("ui-autocomplete");
ac._renderItem = function (ul, item) {
return $("<li></li>")
.data("item.autocomplete", item)
.append("<div>"+item.label+"<a class='link' href='http://test'>(http://test)</a></div>")
.appendTo(ul);
};
var ms = $._data(ac.menu.element.get(0), 'events').menuselect[0].handler;
ac.menu.element.off("menuselect");
ac.menu.element.on("menuselect", function (ev) {
if($(ev.originalEvent.target).hasClass("link"))
return;
ms.apply(this,arguments);
});
答案 4 :(得分:1)
尝试even.stopImmediatePropagation
绑定到onclick
链接。页面正在打开并选择未执行的处理程序。
var li = $("<li></li>")
.data("item.autocomplete", item)
.append('<a class="ac-item-a" href="http://www.nba.com">' + item.label + '</a>')
.appendTo(ul);
li.find('a').bind('click', function(event){ event.stopImmediatePropagation() });
return li;