Bde*_*lle 10 html javascript css jquery google-chrome
我有一些代码在JSFiddle中工作但我无法在我自己的页面中运行.
HTML
<body style="background-color: #000000">
<form oninput="amount.value=range.value" style="color:#1ec2c5;">
<output name="amount" for="range">2</output><a> KM</a>
<input type="range" name="range" min="1" max="9" step="1" value="2" id="test">
</body>
Run Code Online (Sandbox Code Playgroud)
CSS
input[type="range"]{
-webkit-appearance: none;
-moz-apperance: none;
border-radius: 6px;
width: 225px;
height: 6px;
border: 2px solid #eceef1;
outline:none;
background-image: -webkit-gradient(
linear,
left top,
right top,
color-stop(0.15, #eceef1),
color-stop(0.15, #0c0d17)
); }
input[type='range']::-webkit-slider-thumb {
-webkit-appearance: none !important;
background-color: #1ec2c5;
border: 3px solid #000000;
height: 25px;
width: 25px;
border-radius: 20px;}
Run Code Online (Sandbox Code Playgroud)
JavaScript的
$('input[type="range"]').change(function () {
var val = ($(this).val() - $(this).attr('min')) / ($(this).attr('max') - $(this).attr('min'));
$(this).css('background-image',
'-webkit-gradient(linear, left top, right top, '
+ 'color-stop(' + val + ', #eceef1), '
+ 'color-stop(' + val + ', #0c0d17)'
+ ')'
);
});
Run Code Online (Sandbox Code Playgroud)
如果我接受代码并放入HTML/JS(链接在头部)/ CSS(链接在头部)文件中,CSS可以工作,但JavaScript不能.
我已经尝试用$(this)
元素的id 替换但仍然没有运气.
nnn*_*nnn 12
"如果我接受代码并放入HTML/JS(链接在头部)"
问题是你已经把你的代码放在了<head>
,所以它在解析输入元素之前运行,因此$('input[type="range"]')
没有找到任何元素.
如果你看一下JSFiddle中的"Frameworks&Extensions"选项,你会注意到它onload
默认将你的JS代码放在一个处理程序中,所以你的代码在整个页面加载之前都不会运行.要使代码在您自己的网页上以相同的方式运行,您需要将其包装在onload
您自己的处理程序中,或者 - 因为您正在使用jQuery - 将其包装在文档就绪处理程序中:
$(document).ready(function() {
// your code here
});
Run Code Online (Sandbox Code Playgroud)
或者简短形式是这样的:
$(function() {
// your code here
});
Run Code Online (Sandbox Code Playgroud)
或者在页面末尾包含您的脚本,以便在尝试操作的元素被解析后运行.
归档时间: |
|
查看次数: |
7065 次 |
最近记录: |