如何在新选项卡中打开的网站中自动点击按钮



下面需要您的帮助,下面所有代码都来自"example.com"假设:

<a href="http://www.example.org" target="vo1" onclick="gp(1)" rel="nofollow">Click Me</a>

当我点击" click Me",它将运行函数"gp()"在

<script>
function gp(pul){
var qal = "vo";
var tul = document.querySelectorAll('[target="'+qal+pul+'"]');    
[].slice.call(tul).forEach(
function(link){
setTimeout(function(){
if(pul == 1){
window.open("http://www.example.com",qal+pul);
}else
if(pul == 2){
window.open("http://www.example.com",qal+pul);
}
},1000);
}
);
}
</script>

我应该在上面的脚本中添加什么脚本来自动点击"testbutton"下面当"example.com"加载在新选项卡上吗?我需要弹出的消息出现在新的选项卡。

下面所有的代码都是为弹出消息。

<button style="display:none;" id="myBtn">testbutton</button>

<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<div class="modal-header">
<span class="close">&times;</span>
<h2>Modal Header</h2>
</div>
<div class="modal-body">
<p>Some text in the Modal Body</p>
<p>Some other text...</p>
</div>
<div class="modal-footer">
<h3>Modal Footer</h3>
</div>
</div>
</div>

<script>
// Get the modal
var modal = document.getElementById("myModal");
// Get the button that opens the modal
var btn = document.getElementById("myBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal 
btn.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
<style>
body {font-family: Arial, Helvetica, sans-serif;}
/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}
/* Modal Content */
.modal-content {
position: relative;
background-color: #fefefe;
margin: auto;
padding: 0;
border: 1px solid #888;
width: 80%;
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
-webkit-animation-name: animatetop;
-webkit-animation-duration: 0.4s;
animation-name: animatetop;
animation-duration: 0.4s
}
/* Add Animation */
@-webkit-keyframes animatetop {
from {top:-300px; opacity:0} 
to {top:0; opacity:1}
}
@keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
/* The Close Button */
.close {
color: white;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
.modal-header {
padding: 2px 16px;
background-color: #5cb85c;
color: white;
}
.modal-body {padding: 2px 16px;}
.modal-footer {
padding: 2px 16px;
background-color: #5cb85c;
color: white;
}
</style>

基本的想法是,我在urlA,当我点击一个链接,它会加载相同的urlA在新的选项卡。在这个新选项卡中,当它加载时,我希望它自动单击按钮以触发弹出消息。提前感谢你的帮助。

自动点击是什么意思?

新标签,它打开一个网站由你吗?


你应该使用url参数,我会为你做一个非常基本的例子,你必须改进这个,如果你想在它有更多的功能。

对于html,使用:

<a target="_blank" href="http://example.com/index.html?clickButton=true">Link</a>

将参数添加到href link中,clickbutton=true

对于JS,你只需这样写:

function buttonClick() {
alert('pop up')
}
let url = window.location.href;
let params = url.split('?');
let myparam = params[1].split('=');
if (myparam[1] === 'true') buttonClick();

我刚刚得到了url字符串,并使用了一些拆分函数来获得参数的结果。肯定有特定的函数可以更好地做到这一点,但我从未使用过它们,所以我给你一些基本的JS代码。

最新更新