我的问题是当我点击刷新页面时刷新页面它保持在顶部而不滚动到具有id形式的div?
jQuery的:
$(document).ready(function(){
if($('label').length > 0){
$('label').each(function(){
if($(this).css('color') == 'red'){
alert('test');
$('html, body').animate({
scrollTop: $("form").offset().top
}, 1000);
}
});
}
});
HTML:
<form enctype="multipart/form-data" method="post" action="" name="ats_form">
<div id="form" class=" ui-body ui-body-d"> <!-- element where I want to scroll -->
<fieldset>
<legend>
<b>Kontaktní osoba</b>
</legend>
<div data-role="fieldcontain">
<label for="f_FName">Jméno *</label> <!-- if this label is red, php script check input with regular expression when it is not filled or name is not enter properly then it set this label attribute css to color: red; -->
<input id="f_FName" class="ui-input-text ui-body-d ui-corner-all ui-shadow-inset" type="text" value="" size="30" name="f_FName"> <!-- this is input that php check -->
</div>
</fieldset>
</div>
<input class="btnform ui-btn-hidden" type="submit" name="Submit" value="Odeslat" aria-disabled="false">
</form>
如果输入例如为空或具有错误值,则在PHP验证后使用HTML: 与以前相同的结构 Jméno* 与以前相同的结构
答案 0 :(得分:2)
if ($('label[style*="color:red"]').length > 0) { //If there's a label with style*="color:red"...
alert('scroll'); //...Alert
$('html, body').animate({ //..animate to form
scrollTop: $("form").offset().top
}, 1000);
};
将其放入$(document).ready
- 事件