简体   繁体   English

使用JavaScript获取/附加属性(无jQuery)

[英]Get/Append Attribute with JavaScript (no jQuery)

I'm working on an input slider function. 我正在使用输入滑块功能。 I have it successfully working with jQuery, but I want to do it with pure JS. 我已经成功使用jQuery,但是我想使用纯JS。

Here's a fiddle http://jsfiddle.net/PbED3/ . 这是一个小提琴http://jsfiddle.net/PbED3/ Code is as follows: 代码如下:

HTML 的HTML

<div class="image">
  <img src="http://s23.postimg.org/78bouskgb/tumblr_n381qg_QRTS1st5lhmo1_1280.jpg" />    
</div>

<div class="slider">
  <input type="range" id="slider" min="0" max="100" onchange="rangevalue.value=value" value="100"/>
  <output id="rangevalue">100</output>
</div>

JS (jQuery) JS(jQuery)

$(function() {

$("#slider").change(function(e) {   
        $(".image").css("height", $(this).val() * 2 + "px");    
    });

});

As you an see, the function gets the input value from the slider and appends it to the image element as a CSS height value. 如您所见,该函数从滑块获取输入值,并将其作为CSS高度值附加到图像元素。

I want to do this without Jquery. 我想在没有Jquery的情况下执行此操作。

So far my function is this: 到目前为止,我的功能是这样的:

//Pure JS
var value = document.getElementById("#slider").value;
var image = document.getElementsByClassName('.image');

image.style.height= value + "px";

Here is how it would work, the HTML: HTML的工作方式如下:

<div class="image">
    <img src="http://s23.postimg.org/78bouskgb/tumblr_n381qg_QRTS1st5lhmo1_1280.jpg" />    
</div>

<div class="slider">
    <input type="range" id="slider" min="0" max="100" value="100"/>
    <output id="rangevalue">100</output>
</div>

And the javascript: 和javascript:

var slider = document.getElementById("slider");
var rangevalue = document.getElementById("rangevalue");
var image = document.querySelector('.image');

slider.onchange = function () {
    image.style.height = (slider.value * 2) + 'px';
    rangevalue.value = slider.value;
};

The thing with your code is that you are using CSS selector terminology with document.getElementById , which just needs the id of the element. 代码的问题在于,您正在将CSS选择器术语与document.getElementById ,仅需要元素的ID。 If you want to use CSS selectors as you do with jQuery then use document.querySelector (grabs the first element that corresponds to the selector) or document.querySelectorAll . 如果要像使用jQuery一样使用CSS选择器,请使用document.querySelector (抓住与选择器相对应的第一个元素)或document.querySelectorAll And also, you needed to place your callback inside an onchange event handler of the slider element. 而且,您需要将回调放置在slider元素的onchange事件处理程序中。

You were getting there, here is a working example without jQuery: 您到了那里,这是一个没有jQuery的有效示例:

var slider =  document.getElementById("slider");

if (slider.addEventListener) {
    slider.addEventListener("change", slideHandler, false);
} else {
    slider.attachEvent('onchange', slideHandler);
}  

function slideHandler() {
    var value = slider.value;
    var image = document.getElementsByClassName('image')[0];
    if (image) {
        image.style.height= value + "px";
    }
}

Main issues were than you needed to bind the event ( addEventListener or attachEvent ) and you were still passing jQuery style selectors to the getElementById and getElementsByClassName functions. 主要问题超出了绑定事件所需的范围( addEventListenerattachEvent ),并且仍在将jQuery样式选择器传递给getElementByIdgetElementsByClassName函数。 Also, getElementsByClassName returns an array so you only want to update the first instance found. 另外, getElementsByClassName返回一个数组,因此您只想更新找到的第一个实例。

Updated Fiddle 更新小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM