link 上的触发事件不起作用
Trigger event on link wont work
大家好,我正在构建一个库,例如,我给 html 元素赋予了一个属性
<a href="https://google.com" shc="27">
logout
像这个 shc="27" 这意味着当在键盘上单击键 27 (ESC) 时,它将触发对 link 的单击,但由于某种原因它拒绝单击它。
这是我的完整代码:
$(document).on("keydown", function(e) {
console.log(e.which);
checkElementType(e.which);
});
function checkElementType(shortcutKey){
var element = $("[shc="+shortcutKey+"]");
var elementType = element.prop('nodeName');
switch(elementType){
case 'A':
console.log(element);
console.log('click the link');
element.trigger('click');
break;
}
}
这里是 fiddle : Fiddle
我什至不知道我更改了什么但它有效
<html>
<body>
<a href="http://google.com" class="shortcut" shc="27">
logout
</a>
</body>
</html>
$(document).on("keydown", function(e) {
console.log(e.which);
checkElementType(e.which);
});
function checkElementType(shortcutKey){
var element = $("[shc="+shortcutKey+"]");
var elementType = element.prop('nodeName');
switch(elementType){
case 'A':
element.trigger('click');
console.log(element);
console.log('click the link');
break;
}
}
您可以使用 window.location.href
属性打开基于元素 href
属性的 link:
window.location.href = element.attr('href');
希望对您有所帮助。
$(document).on("keydown", function(e) {
console.log(e.which);
checkElementType(e.which);
});
function checkElementType(shortcutKey){
var element = $("[shc="+shortcutKey+"]");
var elementType = element.prop('nodeName');
switch(elementType){
case 'A':
console.log(element);
console.log('click the link');
window.location.href = element.attr('href');
//Or
//element[0].click();
//Or
//element[0].trigger('click');
break;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="http://google.com" class="shortcut" shc="27">logout</a>
将element.trigger('click');
更改为element[0].click()
大家好,我正在构建一个库,例如,我给 html 元素赋予了一个属性
<a href="https://google.com" shc="27">
logout
像这个 shc="27" 这意味着当在键盘上单击键 27 (ESC) 时,它将触发对 link 的单击,但由于某种原因它拒绝单击它。 这是我的完整代码:
$(document).on("keydown", function(e) {
console.log(e.which);
checkElementType(e.which);
});
function checkElementType(shortcutKey){
var element = $("[shc="+shortcutKey+"]");
var elementType = element.prop('nodeName');
switch(elementType){
case 'A':
console.log(element);
console.log('click the link');
element.trigger('click');
break;
}
}
这里是 fiddle : Fiddle
我什至不知道我更改了什么但它有效
<html>
<body>
<a href="http://google.com" class="shortcut" shc="27">
logout
</a>
</body>
</html>
$(document).on("keydown", function(e) {
console.log(e.which);
checkElementType(e.which);
});
function checkElementType(shortcutKey){
var element = $("[shc="+shortcutKey+"]");
var elementType = element.prop('nodeName');
switch(elementType){
case 'A':
element.trigger('click');
console.log(element);
console.log('click the link');
break;
}
}
您可以使用 window.location.href
属性打开基于元素 href
属性的 link:
window.location.href = element.attr('href');
希望对您有所帮助。
$(document).on("keydown", function(e) {
console.log(e.which);
checkElementType(e.which);
});
function checkElementType(shortcutKey){
var element = $("[shc="+shortcutKey+"]");
var elementType = element.prop('nodeName');
switch(elementType){
case 'A':
console.log(element);
console.log('click the link');
window.location.href = element.attr('href');
//Or
//element[0].click();
//Or
//element[0].trigger('click');
break;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="http://google.com" class="shortcut" shc="27">logout</a>
将element.trigger('click');
更改为element[0].click()