Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中添加img响应类_Javascript_Jquery_Html_Css_Angularjs - Fatal编程技术网

如何在javascript中添加img响应类

如何在javascript中添加img响应类,javascript,jquery,html,css,angularjs,Javascript,Jquery,Html,Css,Angularjs,我正在尝试为手机版制作一个响应速度快的滑块。 该网站是使用AngularJS开发的。 当我尝试集成JQuery滑块时,由于引导CSS文件,整个站点都被破坏了。 因此,在这一部分中,我创建了一个简单的Javascript代码。在这方面,我们将如何使这些图像具有响应性。 下面我添加代码 <head> <script type="text/javascript"> var slideimages = new Array() // create new a

我正在尝试为手机版制作一个响应速度快的滑块。 该网站是使用AngularJS开发的。 当我尝试集成JQuery滑块时,由于引导CSS文件,整个站点都被破坏了。 因此,在这一部分中,我创建了一个简单的Javascript代码。在这方面,我们将如何使这些图像具有响应性。 下面我添加代码

    <head>
    <script type="text/javascript">
    var slideimages = new Array() // create new array to preload images
    slideimages[0] = new Image() // create new instance of image object
    slideimages[0].src = "images/slide/1.jpg" // set image object src property to an image's src, preloading that image in the process
    slideimages[1] = new Image()
    slideimages[1].src = "images/slide/2.jpg"
    slideimages[2] = new Image()
    slideimages[2].src = "images/slide/2.jpg"
    </script>
    </head>
    <body>
    <a href="javascript:slidelink()"><img src="firstcar.gif" id="slide" width=100 height=56 /></a>

<script type="text/javascript">

//variable that will increment through the images
var step = 0
var whichimage = 0

function slideit(){
 //if browser does not support the image object, exit.
 if (!document.images)
  return
 document.getElementById('slide').src = slideimages[step].src
 whichimage = step
 if (step<2)
  step++
 else
  step=0
 //call function "slideit()" every 2.5 seconds
 setTimeout("slideit()",2500)
}

function slidelink(){
 if (whichimage == 0)
  window.location = "#"
 else if (whichimage == 1)
  window.location = "#"
 else if (whichimage == 2)
  window.location = "#"
}

slideit()

</script>

要添加类,只需使用addClass:

slideimages[0].addClass("img-responsive");
您只是想添加一个类吗?我不太明白你的问题。

你只需要这样做:

function slideit(){
 //if browser does not support the image object, exit.
 if (!document.images)
  return
 document.getElementById('slide').src = slideimages[step].src
document.getElementById('slide').className = "img-responsive";
 whichimage = step
 if (step<2)
  step++
 else
  step=0
 //call function "slideit()" every 2.5 seconds
 setTimeout("slideit()",2500)
}

前面的答案是正确的,但jQuery必须使用$:

$(slideimages[0]).addClass("img-responsive");
没有jQuery:

slideimages[0].className += "img-responsive";

document.location=https://www.google.nl/webhp?tab=ww&ei=d8p5VPSTLMfkUpf7gpgE&ved=0CAkQ1S4safe=off&q=How+to+添加+a+类+in+javascript@D.M.Vamsi使用此更改您的slideit方法并重试。没有用。。没有影响。那么只需:slideimages[0]。className+=img responsive;但是滑动图像并没有得到。。不知道为什么。。剧本很好。。但是图像不会滑动。。这里是链接。你能帮我摆脱困境吗。。谢谢,我不知道你想做什么,但你可以先修复javascript错误,如果你查看一下浏览器控制台,发现有一些脚本错误和未找到的文件,第一个脚本会给你一个错误,因为你在加载库之前使用了$jQuery,尝试用window.onload事件或jQuery的等效事件包装它。