javascript 如何在javascript中将数据属性值转换为整数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22990503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert data attribute value to integer for in javascript?
提问by Alex
I've been searching everywhere for this and can't seem to find an anwser that works. What i have is a slider with arrows and buttons, when an arrow or button is clicked the function grabs the data-cs attribute which it uses to to determine the next slide.
我一直在到处寻找这个,似乎找不到一个有效的 anwser。我拥有的是一个带有箭头和按钮的滑块,当单击箭头或按钮时,该函数会抓取 data-cs 属性,用于确定下一张幻灯片。
the html
html
<div id="arrowleft" class="sliderbtns flip" data-sc="prev"></div>
<div id="arrowright" class="sliderbtns" data-sc="next"></div>
<div id="buttons" class="sliderbtns">
<div class="selectedbtn sliderbtns" id="button1" data-sc="1"></div>
<div class="button sliderbtns" data-sc="2"></div>
</div>
the javascript
javascript
$( ".sliderbtns" ).click(function() {
var button_data = $(this).attr('data-sc');
myslider(button_data);
});
the function in js
js中的函数
function myslider(position) {
if (position == "next" && (currentslide == slidecount)) {
position = 1;
} else if (position == "next") {
position = currentslide + 1;
} else if ((position == "prev") && (currentslide == 1)){
position = slidecount;
} else if ((position == "prev")) {
position = position - 1;
} else {
// position must then eqauls some integer from the data-sc attribute
position = parseInt(position);
alert (position); // alerts properly the first time
var out_slide = "Slide" + currentslide + "Out";
var next_slide = "Slide" + position;
alert(out_slide); //these work fine the first time
alert(next_slide);
window[next_slide]();
window[out_slide]();
// later on in the slide currentslide becomes position after this it returns value of Nan
setTimeout( function() {
currentslide = position;
alert("this is the current slide position" + currentslide);
}, 2000);
/* ommited code */
}
}
The arrows work fine if I just click on the arrows but when I click on a button it proprly goes to the next slide but none of the buttons no longer work and display current position as NaN, I have tried making the position variable an int in a bunch of different ways with Number(position), ToInt and ParseInt, but still after currentslide = position it returns it as Nan and the slider can no longer find the proper slides and functions etc... If any one has any idea what I could be doing wrong, how to properly make sure it's an actuall integer if that's posssible from a data attribute, or maybe some other way around I can throw that data around as and int that could be gotten by clicking the arrows or buttons. Greatly appreciate any help!
如果我只点击箭头,箭头就可以正常工作,但是当我点击一个按钮时,它会正确地转到下一张幻灯片,但没有一个按钮不再工作并将当前位置显示为 NaN,我尝试将位置变量设置为 int使用 Number(position)、ToInt 和 ParseInt 有多种不同的方式,但在 currentslide = position 之后它仍将其返回为 Nan 并且滑块无法再找到正确的幻灯片和功能等......如果有人知道我是什么可能做错了,如何正确地确保它是一个实际的整数,如果这可能来自数据属性,或者也许我可以通过其他方式将数据抛出 as 和 int 可以通过单击箭头或按钮获得。非常感谢任何帮助!
回答by Milind Anantwar
To get data use .data()
:
获取数据使用.data()
:
$(this).data('sc')
回答by jancha
Seems nobody reading the question.
似乎没有人阅读这个问题。
NaN should appear due to this:
由于这个原因,应该出现 NaN:
position = currentslide + 1;
From code, I cannot see currentslide
being intialized. If you do undefined + 1
, you get NaN
.
从代码中,我看不到currentslide
正在初始化。如果你这样做undefined + 1
,你会得到NaN
。
Globally, set currentSlide = 0
to have it work properly. Unless you're hiding code.
在全球范围内,设置currentSlide = 0
使其正常工作。除非你隐藏代码。
ps. title does not match the question posed in the description.
附:标题与描述中提出的问题不匹配。