我想编写一个代码,如果您按下按钮并且用户名和密码正确。然后会显示一张表格,其中包含照片和指向其Instagram的链接。
在 script 标记中的 功能 内使用 table 标记时函数损坏,并且html本身没有函数。所以当按下按钮时,我应该在哪里显示我的桌子?
这是我的代码:
<html>
<head>
<title>balalar</title>
<script type="text/javascript">
function check(){
var username=document.getElementById("username").value;
var password=document.getElementById("password").value;
if(username == "user1" && password == "1234"){
alert("hello user")
<!--want to put the table here. but it breaks the function-->
}
else
{
alert("chek username and password");
}
}
</script>
</head>
<body>
<form align="center">
username:<input type="text" name="username" id="username" /><br>
</br>
password:<input type="password" name="password" id="password"/><br>
</br>
<input type="button" value="login" id="login" onclick="return
check()"/>
</form>
</body>
</html>
这是我的桌子:
<!DOCTYPE html>
<html>
<link rel=stylesheets; href="css.css">
<head>
<title>BALALAR</title>
<style>
table, th, td {
border:5px solid black;
border-collapse: collapse;
}
th, td {
padding: 30px;
}
tr:nth-child(even) {
background: pink;
}
th {
background: gray;
}
</style>
</head>
<body>
<table align="center">
<caption style="color: #ff99ac"> <h1>[BALALAR]</h1> </caption>
<tr> <!--heading------------------------------------------------------------>
<th>photo</th>
<th>name</th>
<th>age</th>
<th>info</th>
<th>instagram</th>
</tr>
<tr> <!--lucifer------------------------------------------------------------>
<td> <center> <img src="https://scontent-ams3-1.cdninstagram.com/vp/17d60601e17a27d3a0196470f00c55a4/5CF8DA8C/t51.2885-15/e35/50850072_2165205533518446_3117212119304625148_n.jpg?_nc_ht=scontent-ams3-1.cdninstagram.com" height="200px" width="200px"> </center> </td>
<td> <center>lucifersexdoll</center> </td>
<td> <center>18</center> </td>
<td> <center>instagram star and tiktoker</center> </td>
<td> <center> <a href="https://www.instagram.com/lucifersexdoll/?hl=en"> <img src="https://www.edigitalagency.com.au/wp-content/uploads/new-instagram-logo-png-transparent.png" height="50px" width="50px"> </a> </center> </td>
</tr>
<tr> <!--belle delphine----------------------------------------------------->
<td> <center> <img src="https://scontent-dfw5-1.cdninstagram.com/vp/6331b02f48e920975e98434c595fd4ee/5CE47C32/t51.2885-15/e35/41760483_210825032975228_2673466538809180596_n.jpg?_nc_ht=scontent-dfw5-1.cdninstagram.com" height="200px" width="170px"> </center> </td>
<td> <center>belle delphine</center> </td>
<td> <center>19</center></td>
<td> <center>instagram star and snapchat thot</center></td>
<td> <center> <a href="https://www.instagram.com/belle.delphine/?hl=en"> <img src="https://www.edigitalagency.com.au/wp-content/uploads/new-instagram-logo-png-transparent.png" height="50px" width="50px"> </a> </center> </td>
</tr>
<tr> <!--Georgia Twinn------------------------------------------------------>
<td> <center> <img src="https://scontent-frx5-1.cdninstagram.com/vp/b4cdaabe45fd41c4d36e18c074a0cb71/5CEFBA3C/t51.2885-15/e35/43986219_288982548424250_1512646608107907870_n.jpg?_nc_ht=scontent-frx5-1.cdninstagram.com" style="transform: rotate(-90deg);" height="200px" width="200" > </center> </td>
<td> <center>Georgia Twinn</center> </td>
<td> <center>15</center> </td>
<td> <center> instagram star and youtuber</center> </td>
<td> <center> <a href="https://www.instagram.com/georgialeetwinn/"> <img src="https://www.edigitalagency.com.au/wp-content/uploads/new-instagram-logo-png-transparent.png" height="50px" width="50px" > </a> </center> </td>
</tr>
<tr> <!--lilsaggester------------------------------------------------------->
<td> <center> <img src="https://scontent-ams3-1.cdninstagram.com/vp/b7b351d27e76a70aed0dc97ca2946b31/5CF7E289/t51.2885-15/e35/44478694_259861178046212_7053862547100372241_n.jpg?_nc_ht=scontent-ams3-1.cdninstagram.com" height="200px" width="200px" > </center> </td>
<td> <center>hannah fabel</center> </td>
<td> <center>16</center> </td>
<td> <center>tiktoker</center> </td>
<td> <center> <a href="https://www.instagram.com/aestheticallyhannah/"> <img src="https://www.edigitalagency.com.au/wp-content/uploads/new-instagram-logo-png-transparent.png" height="50px" width="50px" > </a> </center> </td>
</tr>
<tr> <!--cyxll-------------------------------------------------------------->
<td> <center> <img src="https://scontent-ams3-1.cdninstagram.com/vp/fe0da5e3353e102fd30a771e7f18d793/5CEA7C9B/t51.2885-15/e35/50510784_321000775215940_7823002975742791621_n.jpg?_nc_ht=scontent-ams3-1.cdninstagram.com" height="200px" width="250px"> </center> </td>
<td> <center>cyxll</center> </td>
<td> <center>unknown</center> </td>
<td> <center>instagram user</center> </td>
<td> <center> <a href="https://www.instagram.com/cyxll/?hl=en"> <img src="https://www.edigitalagency.com.au/wp-content/uploads/new-instagram-logo-png-transparent.png" height="50px" width="50px"> </a> </center> </td>
</tr>
<tr> <!--gal gadot---------------------------------------------------------->
<td> <center><img src="https://scontent-ams3-1.cdninstagram.com/vp/85ba393e3d05f6047fea8ac9ee61edd4/5D250597/t51.2885-15/e35/40265259_229384887926581_2920908417464813508_n.jpg?_nc_ht=scontent-ams3-1.cdninstagram.com" height="200px" width="200px"> </center> </td>
<td> <center>gal gadot</center> </td>
<td> <center>33</center> </td>
<td> <center>actress</center> </td>
<td> <center> <a href="https://www.instagram.com/gal_gadot/?hl=en"> <img src="https://www.edigitalagency.com.au/wp-content/uploads/new-instagram-logo-png-transparent.png" height="50px" width="50px"> </a></center> </td>
</tr>
</table>
</body>
</html>
以后想为不同的用户放置不同的表。
答案 0 :(得分:1)
您可以将表格保留在HTML正文中……并制作#tableID {display: none;}
在JS中:
function check(){
var username=document.getElementById("username").value;
var password=document.getElementById("password").value;
if(username == "user1" && password == "1234"){
alert("hello user")
document.getElementById('tableID').style.display = 'block';
}
else {alert("chek username and password");}
}
但是,即使代码太具体,当必须将HTML放在JS中时,也必须转义所有特殊字符(搜索> JavaScript字符串转义)
大多带有符号\
...有时带有[]
或HTML-code
例如,
<td> <center> <img src="https://scontent-ams3-1.cdninstagram.com/vp/17d60601e17a27d3a0196470f00c55a4/5CF8DA8C/t51.2885-15/e35/50850072_2165205533518446_3117212119304625148_n.jpg?_nc_ht=scontent-ams3-1.cdninstagram.com" height="200px" width="200px"> </center> </td>
必须替换为:
<td> <center> <img src="https:\/\/scontent-ams3-1.cdninstagram.com\/vp\/17d60601e17a27d3a0196470f00c55a4\/5CF8DA8C\/t51.2885-15\/e35\/50850072_2165205533518446_3117212119304625148_n.jpg?_nc_ht=scontent-ams3-1.cdninstagram.com" height=\'200px\' width=\'200px\'> <\/center> <\/td>
您可以在这里尝试这样的操作--https://liveweave.com/ 它具有舒适的颜色,可以告诉您是否有错误。
P.s。 “您能再解释一下吗?”:
<table id="AnyName"> ... </table>
每个HTML元素可以具有唯一的ID,该ID可以通过JavaScript定位。
另外,如果您使用.class {}
选择CSS中的类,则可以使用#id {}
选择ID。因此,我们使用#AnyName {display: none}
然后单击...,如果用户名和密码匹配-我们通过ID获取元素并使其可见:
document.getElementById('AnyName').style.display = 'block';
显示“使用此ID替换元素,并更改其样式”
可能有.style.border = '1px solid red';
或.style.backgroundColor = 'red';
但是我们需要使其可见...因此,display = 'block';
答案 1 :(得分:0)
有很多方法可以实现这一目标。我有两个给你:
下面是一些代码:
popular = open("C:/Users/NAME/FilesForPyCharm/popular.txt")
words = [line.strip() for line in popular]
poss = [j.split() for j in [decipher(i, txt) for i in range(1, 26)]]
for k in poss:
if set(k).issubset(set(words)):
return f"The key is {i} and the deciphered text is{' '.join(k)}."
function getAjax(url, success) {
var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
xhr.open('GET', url);
xhr.onreadystatechange = function() {
if (xhr.readyState>3 && xhr.status==200) success(xhr.responseText);
};
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.send();
return xhr;
}
// example request
//getAjax('http://foo.bar/?p1=1&p2=Hello+World', function(data){ console.log(data); });
对于两个示例,您都需要一个名为<html>
<head>
<title>balalar</title>
<script type="text/javascript">
function check(){
var username=document.getElementById("username").value;
var password=document.getElementById("password").value;
if(username == "user1" && password == "1234"){
alert("hello user")
// 1. Solution - Redirect to another "Page"
window.location.href = "/table.html?username="+username;
// 2. Solution - do an Ajax Call and render response
getAjax("/table.html?username="+username, function(response){
// replace body with new content ("table")
document.getElementsByTagName('body')[0].innerHTML = response;
});
}
else
{
alert("chek username and password");
}
}
</script>
</head>
<body>
<form align="center">
username:<input type="text" name="username" id="username" /><br>
</br>
password:<input type="password" name="password" id="password"/><br>
</br>
<input type="button" value="login" id="login" onclick="return
check()"/>
</form>
</body>
</html>
的文件,其表内容位于table.html
旁边
答案 2 :(得分:0)
我看到您将表格和登录页面留在2个不同的html文件中。将其放在html页面中并在检查功能()中处理:
function check(){
var username=document.getElementById("username").value;
var password=document.getElementById("password").value;
if(username == "user1" && password == "1234"){
alert("hello user")
<!--want to put the table here. but it breaks the function-->
document.getElementsByTagName("table")[0].style.display = "block";
document.getElementsByTagName("form")[0].style.display = "none";
}
else
{
alert("chek username and password");
}
}
<html>
<head>
<title>balalar</title>
<style>
table, th, td {
border:5px solid black;
border-collapse: collapse;
}
th, td {
padding: 30px;
}
tr:nth-child(even) {
background: pink;
}
th {
background: gray;
}
table{
display: none
}
</style>
</head>
<body>
<form align="center">
username:<input type="text" name="username" id="username" /><br>
</br>
password:<input type="password" name="password" id="password"/><br>
</br>
<input type="button" value="login" id="login" onclick="check()"/>
</form>
<table align="center">
<caption style="color: #ff99ac"> <h1>[BALALAR]</h1> </caption>
<tr> <!--heading------------------------------------------------------------>
<th>photo</th>
<th>name</th>
<th>age</th>
<th>info</th>
<th>instagram</th>
</tr>
<tr> <!--lucifer------------------------------------------------------------>
<td> <center> <img src="https://scontent-ams3-1.cdninstagram.com/vp/17d60601e17a27d3a0196470f00c55a4/5CF8DA8C/t51.2885-15/e35/50850072_2165205533518446_3117212119304625148_n.jpg?_nc_ht=scontent-ams3-1.cdninstagram.com" height="200px" width="200px"> </center> </td>
<td> <center>lucifersexdoll</center> </td>
<td> <center>18</center> </td>
<td> <center>instagram star and tiktoker</center> </td>
<td> <center> <a href="https://www.instagram.com/lucifersexdoll/?hl=en"> <img src="https://www.edigitalagency.com.au/wp-content/uploads/new-instagram-logo-png-transparent.png" height="50px" width="50px"> </a> </center> </td>
</tr>
<tr> <!--belle delphine----------------------------------------------------->
<td> <center> <img src="https://scontent-dfw5-1.cdninstagram.com/vp/6331b02f48e920975e98434c595fd4ee/5CE47C32/t51.2885-15/e35/41760483_210825032975228_2673466538809180596_n.jpg?_nc_ht=scontent-dfw5-1.cdninstagram.com" height="200px" width="170px"> </center> </td>
<td> <center>belle delphine</center> </td>
<td> <center>19</center></td>
<td> <center>instagram star and snapchat thot</center></td>
<td> <center> <a href="https://www.instagram.com/belle.delphine/?hl=en"> <img src="https://www.edigitalagency.com.au/wp-content/uploads/new-instagram-logo-png-transparent.png" height="50px" width="50px"> </a> </center> </td>
</tr>
<tr> <!--Georgia Twinn------------------------------------------------------>
<td> <center> <img src="https://scontent-frx5-1.cdninstagram.com/vp/b4cdaabe45fd41c4d36e18c074a0cb71/5CEFBA3C/t51.2885-15/e35/43986219_288982548424250_1512646608107907870_n.jpg?_nc_ht=scontent-frx5-1.cdninstagram.com" style="transform: rotate(-90deg);" height="200px" width="200" > </center> </td>
<td> <center>Georgia Twinn</center> </td>
<td> <center>15</center> </td>
<td> <center> instagram star and youtuber</center> </td>
<td> <center> <a href="https://www.instagram.com/georgialeetwinn/"> <img src="https://www.edigitalagency.com.au/wp-content/uploads/new-instagram-logo-png-transparent.png" height="50px" width="50px" > </a> </center> </td>
</tr>
<tr> <!--lilsaggester------------------------------------------------------->
<td> <center> <img src="https://scontent-ams3-1.cdninstagram.com/vp/b7b351d27e76a70aed0dc97ca2946b31/5CF7E289/t51.2885-15/e35/44478694_259861178046212_7053862547100372241_n.jpg?_nc_ht=scontent-ams3-1.cdninstagram.com" height="200px" width="200px" > </center> </td>
<td> <center>hannah fabel</center> </td>
<td> <center>16</center> </td>
<td> <center>tiktoker</center> </td>
<td> <center> <a href="https://www.instagram.com/aestheticallyhannah/"> <img src="https://www.edigitalagency.com.au/wp-content/uploads/new-instagram-logo-png-transparent.png" height="50px" width="50px" > </a> </center> </td>
</tr>
<tr> <!--cyxll-------------------------------------------------------------->
<td> <center> <img src="https://scontent-ams3-1.cdninstagram.com/vp/fe0da5e3353e102fd30a771e7f18d793/5CEA7C9B/t51.2885-15/e35/50510784_321000775215940_7823002975742791621_n.jpg?_nc_ht=scontent-ams3-1.cdninstagram.com" height="200px" width="250px"> </center> </td>
<td> <center>cyxll</center> </td>
<td> <center>unknown</center> </td>
<td> <center>instagram user</center> </td>
<td> <center> <a href="https://www.instagram.com/cyxll/?hl=en"> <img src="https://www.edigitalagency.com.au/wp-content/uploads/new-instagram-logo-png-transparent.png" height="50px" width="50px"> </a> </center> </td>
</tr>
<tr> <!--gal gadot---------------------------------------------------------->
<td> <center><img src="https://scontent-ams3-1.cdninstagram.com/vp/85ba393e3d05f6047fea8ac9ee61edd4/5D250597/t51.2885-15/e35/40265259_229384887926581_2920908417464813508_n.jpg?_nc_ht=scontent-ams3-1.cdninstagram.com" height="200px" width="200px"> </center> </td>
<td> <center>gal gadot</center> </td>
<td> <center>33</center> </td>
<td> <center>actress</center> </td>
<td> <center> <a href="https://www.instagram.com/gal_gadot/?hl=en"> <img src="https://www.edigitalagency.com.au/wp-content/uploads/new-instagram-logo-png-transparent.png" height="50px" width="50px"> </a></center> </td>
</tr>
</table>
</body>
</html>
如果您只想在表格中显示一行,请为其设置ID,并为具有相应ID的行设置display: block