2013-05-15 110 views
0

我不熟悉Javascript。我一直在試圖弄清楚,但現在我卡住了...根據窗口縱橫比更改src值

我有一個背景滑塊,根據窗口大小不保留其原始方面。避免太多失真,例如在縱向模式下的Ipad。我認爲當窗口的高度大於寬度時,我會爲滑塊使用另一個圖像源。 這是我到目前爲止有:

if(window.innerHeight > window.innerWidth){ 
document.getElementById('threesixty').attr('dataPath','path/to/imagesIpad{index}.jpg'); 
} 
else { 
$('.threesixty').attr('data-path','path/to/images{index}.jpg'); } 

和這裏的什麼是在html

<div class="threesixty" data-path="path/to/images{index}.jpg" data-count="72"> 

但這並不工作...

回答

0

首先, document.getElementById('threesixty')返回dom對象不是jQuery對象,您不能使用.attr()方法。

二,爲什麼在if這是一個ID選擇器,但在else這是一個類選擇器$('.threesixty') ???

三,爲什麼在如果它是attr dataPath但在else它是attr data-path ???

請檢查您的代碼