【问题标题】:How to redirect to a certain URL on a click event in JavaScript?如何在 JavaScript 中的点击事件上重定向到某个 URL?
【发布时间】:2026-01-02 00:25:01
【问题描述】:

单击按钮后,它会打开一个视频。我想在用户单击关闭视频后将其重定向到另一个 URL。 我不熟悉 JavaScript 代码,所以我不知道该怎么做:)

function playVideo () {
    $('#play').click(function () {
        var video = $('#final-video');
        video.css({
            opacity: 1,
            pointerEvents: 'auto'
        })

        video[0].play();

        video.click(function () {
            $(this).css({
                opacity: 0,
                pointerEvents: 'none',
            })
        })
    })
}

【问题讨论】:

  • video 上单击您想要重定向而不是隐藏它?
  • 是的,这就是我想做的事

标签: javascript url redirect click


【解决方案1】:

function playVideo () {
	$('#play').click(function () {
		var video = $('#final-video');
		video.css({
			opacity: 1,
			pointerEvents: 'auto'
		})

		video[0].play();

		video.click(function () {
			$(this).css({
				opacity: 0,
				pointerEvents: 'none'
			})
		})
	})
}
#final-video {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 100000000;
  background: black;
  opacity: 0;
  transition: opacity 0.5s ease-in-out;
  pointer-events: none;
}
		<video id="final-video" src="https://www.youtube.com/watch?v=z3rXoJdu_Ek
"></video>

  <div id="play" class="section-gameplay">
						<button class="button-start" id="button-start2" data-target="flag-part6">You're ready to play</button>
					</div>

【讨论】:

    【解决方案2】:
    function playVideo () {
        $('#play').click(function () {
            var video = $('#final-video');
            video.css({
                opacity: 1,
                pointerEvents: 'auto'
            })
    
            video[0].play();
    
            video.click(function () {
               /* This event fires, when video is loaded and
                  user clicked it. Here you can do your redirect
                  instead of what's being done here */
                $(this).css({
                    opacity: 0,
                    pointerEvents: 'none',
                })
            })
        })
    }
    

    所以它可以是这样的:

    function playVideo () {
        $('#play').click(function () {
            var video = $('#final-video');
            video.css({
                opacity: 1,
                pointerEvents: 'auto'
            })
    
            video[0].play();
    
            video.click(function () {
                window.location('http://*.com');
            })
        })
    }
    

    【讨论】:

    • 现在我无法退出视频,链接仍然无效。
    • 非常感谢您使用正确的 HTML 广告 JS 演示您的问题,看看实际发生了什么。
    • 当然!我是新手,我不明白该怎么做
    【解决方案3】:

    请注意,您也可以在 html 中执行此操作:

    <a href="https://www.example.com">Your text here</a>
    

    【讨论】:

      【解决方案4】:

      在您的关闭视频事件中,您只需添加其中一行代码即可重定向到另一个网址。

      要重定向到另一个页面,您可以使用:

      window.location = "http://www.*.com";
      

      window.location.replace('http://*.com');
      

      如果您想模拟某人点击链接,请使用location.href

      如果要模拟 HTTP 重定向,请使用 location.replace

      在新标签页中重定向

      window.open('http://*.com', '_blank');
      

      【讨论】:

      • 感谢您的回答!我不知道如何将它添加到我的代码中。我只是把它粘贴在里面,但它没有用。你能告诉我怎么做吗? video.click(function () { $(this).css({ opacity: 0, pointerEvents: 'none', window.location.replace('*.com'); }) }) }) }
      • HTML &lt;video id="final-video" src="images/Video_02.mp4"&gt;&lt;/video&gt; CSS #final-video { position: fixed; top: 0; left: 0; width: 100%; height: 100%; z-index: 100000000; background: black; opacity: 0; transition: opacity 0.5s ease-in-out; pointer-events: none; } 我试图做的事情:video.click(function () { $(this).css({ opacity: 0, pointerEvents: 'none' window.location('http://*.com'); }) }) }) }