Mik*_*mes 8 html javascript mobile jquery slider
我在一个简单的页面上有一个jquery移动滑块.拖动滑块时,值会在文本框中按预期更新.我看了,但无法找到这种逻辑发生的地方.
我想要的是将值传递给javascript函数.如何将change事件绑定到我的函数?
干杯
麦克风.
下面的代码 - 请忽略一些讨厌的黑客:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>jQuery Mobile Docs - Forms</title>
<link rel="stylesheet" href="fader/jquery.mobile-1.0a3.css" />
<script type="text/javascript" src="fader/jquery-1.5.js"></script>
<script type="text/javascript" src="fader/jquery.mobile-1.0a3.js"></script>
<script>
$('#slider-1').changed(function () {
alert("Test");
});
</script>
<style type="text/css">
.toolbar {
-webkit-box-sizing: border-box;
border-bottom: 1px solid #000;
padding: 10px;
height: 45px;
background: url(fader/Resources/Themes/JQT/img/toolbar.png) #000000 repeat-x;
position: relative;
}
.toolbar > h1 {
position: absolute;
overflow: hidden;
left: 50%;
top: 10px;
line-height: 1em;
margin: 1px 0 0 -75px;
height: 40px;
font-size: 20px;
width: 150px;
font-weight: bold;
text-shadow: rgba(0, 0, 0, 1) 0 -1px 1px;
text-align: center;
text-overflow: ellipsis;
white-space: nowrap;
color: #fff;
}
.button, .back, .cancel, .add {
position: absolute;
overflow: hidden;
top: 8px;
right: 10px;
margin: 0;
border-width: 0 5px;
padding: 0 3px;
width: auto;
height: 30px;
line-height: 30px;
font-family: inherit;
font-size: 12px;
font-weight: bold;
color: #fff;
text-shadow: rgba(0, 0, 0, 0.5) 0px -1px 0;
text-overflow: ellipsis;
text-decoration: none;
white-space: nowrap;
background: none;
-webkit-border-image: url(fader/Resources/Themes/JQT/img/button.png) 0 5 0 5;
}
body > * {
background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#1e1f21), to(#272729));
}
.back {
left: 6px;
right: auto;
padding: 0;
max-width: 55px;
border-width: 0 8px 0 14px;
-webkit-border-image: url(fader/Resources/Themes/JQT/img/back_button.png) 0 8 0 14;
}
.back.active {
-webkit-border-image: url(Fader%20Test%20-%20Trade%20show/img/back_button_clicked.png) 0 8 0 14;
color: #aaa;
}
h1, h2 {
font: bold 18px Helvetica;
text-shadow: rgba(255, 255, 255, .2) 0 1px 1px;
color: #FFF;
margin: 10px 20px 5px;
}
body {
background: #000;
color: #ddd;
}
</style>
</head>
<body>
<div class="ui-body-a" data-role="page">
<div class="toolbar">
<h1>Input</h1>
<A class="back" HREF="javascript:javascript:history.go(-1)">Home</A
></div>
<form action="#" method="get">
<div class="ui-body-a" data-role="fieldcontain">
<h1>Lighting Intensity</h1>
<input type="range" name="slider-1" id="slider-1" value="0" min="0" max="100" data-theme="b" data-track-theme="a" orientation="vertical" />
</div>
</form>
</div><!-- /content -->
</div><!-- /page -->
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
小智 23
迈克尔.尝试用另一个元素包围滑块:
<div id="div-slider">
<input type="range" id="slider-1" />
</div>
Run Code Online (Sandbox Code Playgroud)
你可以在变化中获得一个事件:
$("#div-slider").change(function() {
var slider_value = $("#slider-1").val();
// do something..
});
Run Code Online (Sandbox Code Playgroud)
您的代码的问题在于,在实际在html中创建之前,会对将元素更改事件绑定到元素的javascript进行评估,因此没有元素按照您的指定绑定到更改事件.
试试这个:
$('#slider-1').live('change', function(){
doYourStuffHere();
});
Run Code Online (Sandbox Code Playgroud)
简而言之,live意味着JQuery将继续将事件绑定到与指定选择器匹配的元素,即使它们在评估JS绑定时不存在,因此此代码将绑定所有与#slider-1选择器匹配的当前和未来 html元素正确改变回调.
另一种选择是使用JQuery'on'绑定,它的行为略有不同,但仍然可以完成这项工作.在这里查看'on'的文档.
| 归档时间: |
|
| 查看次数: |
35643 次 |
| 最近记录: |