我有一个带有3个下拉菜单的html,这是
<select id="day"> <select id="month"> <select id="year">
但每当我选择其中一个值时,选择该选项后,页面将保持回到顶部
我试过跟随类似的情况 How do I stop a web page from scrolling to the top when a link is clicked that triggers JavaScript?
我在我的html中添加一个脚本
<script>
$("#day").click(function(e) {
return false;
e.preventDefault();
});
</script>
这是我的简短html文件
<header class="headerNavi">
<div class="headerNaviLeft">
<a href="#home">Back</a>
</div>
<div class="headerNaviRight">
<a href="#home">Cancel</a>
</div>
</header><!-- End Header -->
<!-- content -->
<section id="formDiv">
<h3>Create Link</h3>
<p class="tl01">Snap a photo or upload your own, then fill up the details
to continue...</p>
<div class="rowBox">
<span class="photoBox">
<img id="imageFile" name="imageFile" src="" style="width:100%; min-height:150px;" onclick="getPhoto(navigator.camera.PictureSourceType.PHOTOLIBRARY);" />
</span>
</div>
<div class="rowBox">
<select id="day">
<option disabled="disabled" selected="selected" value="">
DD
</option>
<option value="1">
1
</option>
// up to 31
</select>
</div>
</section>
这是我的观点
define(['jquery', 'underscore', 'backbone', 'text!modules/link/linkCreateViewTemplate.html'], function($, _, Backbone, linkCreateViewTemplate) {
var LinkCreateView = Backbone.View.extend({
template: _.template(linkCreateViewTemplate),
initialize: function() {
},
events: {
"click #createButton": "create",
},
create: function(event) {
window.location.replace("#linkconfirm");
},
render: function() {
this.$el.append(this.template());
},
remove: function() {
Backbone.View.prototype.remove.call(this);
$(window).off('scroll');
}
});
return LinkCreateView;
});
答案 0 :(得分:1)
尝试将代码包装在DOM就绪处理程序$(function(){...});
$(function() {
$("#day").click(function(e) {
return false;
e.preventDefault();
});
});