我有两页是这样的

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>

和earch.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 :(

推荐答案

将代码注入到子窗口中;

enter image description here

<!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>

Javascript相关问答推荐

我的JS代码将按照哪个序列被解释

想要检测字符串中的所有单词

如何循环访问对象数组并以关键值形式获得结果?

在JS中获取名字和姓氏的首字母

Redux工具包查询(RTKQ)端点无效并重新验证多次触发

传递一个大对象以在Express布局中呈现

如何在RTK上设置轮询,每24小时

Plotly热图:在矩形上zoom 后将zoom 区域居中

jQuery提交按钮重新加载页面,即使在WordPress中使用preventDefault()

React 17与React 18中的不同setState行为

使搜索栏更改语言

在这种情况下,如何 for each 元素添加id?

无法读取未定义错误的属性路径名''

更改预请求脚本中重用的JSON主体变量- Postman

有条件重定向到移动子域

创建以键值对为有效负载的Redux Reducer时,基于键的类型检测

同一类的所有div';S的模式窗口

将数组扩展到对象中

在VS代码上一次设置多个变量格式

ReferenceError:无法在初始化之前访问setData