【发布时间】:2015-03-04 23:27:08
【问题描述】:
我在 2 个 div 元素中有 2 个 span 元素。两个 span 元素都没有 id,两个 div 元素也没有 id。
第一个 div 有第一个带有 id (id_name) 的输入元素,然后是第一个 span 元素。
第二个 div 有第二个带有 id (id_password) 的输入元素,然后是第二个跨度元素。
我有一个 javascript 函数,我在提交表单时调用它。在该函数中,我可以获得变量 element_id_name 中的第一个输入元素和变量 element_id_password 中的第二个输入元素。现在如何获得第一个输入元素之后的第一个跨度元素?以及如何获得第二个输入元素之后的第二个跨度元素?因为我没有跨度元素的 id,所以我不能使用document.getElementById()。有没有办法通过参考第一个输入元素来获取第一个跨度元素?
这是我的代码:
<html>
<head>
<meta charset="ISO-8859-1">
<title>test</title>
<style type="text/css">
.error_noshow{
display: none;
}
.error_show{
color: red;
}
</style>
<script type="text/javascript">
function validate() {
var element_id_name = document.getElementById("id_name");
var element_id_password = document.getElementById("id_password");
return false;
}
</script>
</head>
<body>
<form id="form_login" method="post" action="" onsubmit="validate();">
<div>
<label for="id_name">User Name:</label>
<input type="text" id="id_name" name="txt_user_name">
<span class="error_noshow">Required field</span>
</div>
<div>
<label for="id_password">Password:</label>
<input type="password" id="id_password" name="txt_password">
<span class="error_noshow">Required field</span>
</div>
<button type="submit">Submit</button>
</form>
</body>
</html>
感谢您阅读我的问题。
【问题讨论】:
-
你可以给它们添加类名吗?
标签: javascript html css