在下面的代码中,为什么after事件被触发三次? “文字标记!”在输入框后添加三次。
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("input").select(function(){
$("input").after(" Text marked!");
});
$("button").click(function(){
$("input").trigger("select");
});
});
</script>
</head>
<body>
<input type="text" value="Hello World"><br><br>
<button>Trigger the select event for the input field</button>
</body>
</html>
答案 0 :(得分:5)
我可以确认Chrome中发生了这种情况。我真的不确定原因,但你可以通过添加event.preventDefault()
来修复它。
$("input").select(function(event){
event.preventDefault();
$("input").after(" Text marked!");
});