英文:
Send a keyup event to a child window without changing source of the child
问题
我有两个页面,像这样
test.html
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
</body>
</html>
<script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
<script>
$(document).ready(function(){
var w=window.open("search.html","_blank");
w.onload = function(){
setTimeout(() => {
$('#search', w.document).val("test");
$('#search', w.document).trigger("keyup");
}, 5000);
}
});
</script>
和 search.html
<!DOCTYPE html>
<html>
<head>
<title>Search</title>
</head>
<body>
<input type="text" id="search">
</body>
</html>
<script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
<script>
$(document).ready(function(){
$("#search").keyup(function(){
alert($(this).val());
});
});
</script>
当test.html成功加载时,它会打开一个名为search.html的窗口,并将搜索值设置为"test",然后触发keyup事件。但只有设置值能够生效,无法触发keyup事件。请帮我检查一下
英文:
I have two page like this
test.html
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
</body>
</html>
<script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
<script>
$(document).ready(function(){
var w=window.open("search.html","_blank");
w.onload = function(){
setTimeout(() => {
$('#search', w.document).val("test");
$('#search', w.document).trigger("keyup");
}, 5000);
}
});
</script>
and search.html
<!DOCTYPE html>
<html>
<head>
<title>Search</title>
</head>
<body>
<input type="text" id="search">
</body>
</html>
<script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
<script>
$(document).ready(function(){
$("#search").keyup(function(){
alert($(this).val());
});
});
</script>
When test.html load success, it open window search.html and set search value is test and send event keyup to it. But only set value can happen, keyup cannot send.
Please check for me
答案1
得分: 2
将代码注入子窗口中;
<!DOCTYPE html>
<html>
<head>
<title>测试</title>
</head>
<body>
</body>
</html>
<script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
<script>
$(document).ready(function () {
var w = window.open("search.html", "_blank");
w.onload = function () {
setTimeout(() => {
$('#search', w.document).val("test");
$(`<script>$("#search").trigger('keyup');</script>`).appendTo(w.document.head);
}, 5000);
}
});
</script>
英文:
Inject the code into the child window;
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
</body>
</html>
<script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
<script>
$(document).ready(function () {
var w = window.open("search.html", "_blank");
w.onload = function () {
setTimeout(() => {
$('#search', w.document).val("test");
$(`<script>$("#search").trigger('keyup');<\/script>`).appendTo(w.document.head);
}, 5000);
}
});
</script>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论