javascript - 如何使用文本输入更改 iframe 的 src?

标签 javascript html css iframe

因此,Mojang 发布了一个适用于浏览器的 Minecraft 版本,我一直在尝试将其添加到我的网站。我用 iframe 成功地做到了这一点,但人们想要多人游戏。由于多人游戏通过链接进行,目标是输入他们从其他人那里收到的链接,并将 iframe 从标准的 iframe 更改为新的多人游戏的 iframe。

我尝试的是获取用户输入,将其分配给一个变量,然后使用 document.write 用新的 src 重写现有的 iframe。

这是我得到的。注意:它处于模式中。

input[type=text], input[type=password] {
  width: 300px;
}
.cancelbtn {
  width: auto;
  padding: 10px 18px;
  background-color: #f44336;
}

/* Center the image and position the close button */
.imgcontainer {
  text-align: center;
  margin: 24px 0 12px 0;
  position: relative;
}

img.avatar {
  width: 40%;
  border-radius: 50%;
}

.container {
  padding: 16px;
}

span.psw {
  float: right;
  padding-top: 16px;
}

/* The Modal (background) */
.modal {
  display: none; /* Hidden by default */
  position: fixed; /* Stay in place */
  z-index: 1; /* Sit on top */
  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 */
  padding-top: 60px;
}

/* Modal Content/Box */
.modal-content {
  background-color: #fefefe;
  margin: 5% auto 15% auto; /* 5% from the top, 15% from the bottom and centered */
  border: 1px solid #888;
  width: 80%; /* Could be more or less, depending on screen size */
}

/* The Close Button (x) */
.close {
  position: absolute;
  right: 25px;
  top: 0;
  color: #000;
  font-size: 35px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: red;
  cursor: pointer;
}

/* Add Zoom Animation */
.animate {
  -webkit-animation: animatezoom 0.6s;
  animation: animatezoom 0.6s
}

@-webkit-keyframes animatezoom {
  from {-webkit-transform: scale(0)} 
  to {-webkit-transform: scale(1)}
}
  
@keyframes animatezoom {
  from {transform: scale(0)} 
  to {transform: scale(1)}
}

/* Change styles for span and cancel button on extra small screens */
@media screen and (max-width: 300px) {
  span.psw {
     display: block;
     float: none;
  }
  .cancelbtn {
     width: 100%;
  }
}
.button {
  color: white;
  background-color: #4CAF50;
  border-color: #ccc;
  font-size: 14px;
  font-weight: 400;
  border: 1px solid transparent;
  border-radius: 4px;
  cursor: pointer;
  line-height: 1.42857143;
  text-align: center;
  white-space: nowrap;
  vertical-align: middle;
  padding: 6px 12px;
}
<div id="container">
<div align="center"><iframe id="change" name="change" width="800" height="500" src="//classic.minecraft.net"></iframe></div>
 <div>
 <button class="button" id="button"><b>Fullscreen</b> </button>
 <button class="button" id="modal" onclick="document.getElementById('id01').style.display='block'"><b>Multiplayer</b></button>
 </div>
 <script src="/fullscreen.js"></script>
 <script>
   function changeIframe() {
  document.write(" <iframe  id='change' name='change' src='#" + input + "' width='800'  height='500'   allowfullscreen></iframe>");
}
 </script>
 <script>   
 var input = document.getElementById("detroit");
 </script>
 <div id="id01" class="sprckn-modal">
  
<form class="modal-content animate">
    <span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">&times;</span>
  
  
  <div class="sprckn-modal-content">
    <label for="link"><b>Paste Multiplayer Link Here</b></label>
    <center><input type="text" onsubmit="changeIframe()" id="detroit" placeholder="https://classic.minecraft.net/?join=somelettersand#s" name="link" required></center>
  </div>
  
  <div class="container" style="background-color:#f1f1f1">
    <center><button type="button" onclick="document.getElementById('id01').style.display='none'" class="cancelbtn">Cancel</button></center>
</form>
  </div>
</div>
`

iframe 的任何部分绝对没有变化,但我的网站 url 有变化,带有 ?link=(linkhere)。在 https://sprocken.com/game/minecraft/ 可以更好地展示它.

最佳答案

更改现有的 iframe 源:

function changeIframe(url) {
  document.getElementById('change').src = url;
}

使用onchange 事件代替onsubmit:

<input type="text" id="minecraftInput" onchange="changeIframe(this.value)" id="detroit" placeholder="https://classic.minecraft.net/?join=somelettersand#s" name="link" required>

关于javascript - 如何使用文本输入更改 iframe 的 src?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56370104/

相关文章:

php - 使用隐藏的输入类型防止垃圾邮件

css - 无法在 chrome 中编辑 CSS STYLE 属性(变灰和屏蔽)

html - CSS Footer 100% 宽度添加滚动条

css - 如何避免带有渐变边框的按钮在悬停时跳转

html - Div CSS 中的多个图像

html - 自己的菜单栏 - 一行中只有一个

php - 使用javascript获取服务器信息

javascript - 将元素中的 CSS 与页面的其余部分隔离

javascript - Coldfusion 的 cfpdfform 的 PHP 等价物

javascript - 在文档准备好之前创建 cookie 安全吗?