2016-08-18 50 views
-1

圖像的高度我使用jQuery這樣我的網頁上加載圖像:設置寬度和裝載jQuery的

$(function() { 
    $('#mySelect').change(function() { 
     var selectedCity = $('#mySelect').val(); 
     var myLatLng = new google.maps.LatLng(eval(selectedCity).lan, eval(selectedCity).lng); 
     venueMap.setCenter(myLatLng); 
     image = 'img.png'; 
     marker = new google.maps.Marker({ 
      position: myLatLng, 
      map: venueMap, 
      icon: image 
     }); 
     marker.setMap(venueMap); 
    }); 
}); 

我怎麼可以設置寬度和圖像的高度?

我試過 image.width = 100;image.height = 50;(作爲js語句)沒有成功。

至於問我相關的HTML代碼:

.... 
<body> 
     <center> 
      <h2>Testing jQuery with Google Maps</h2> 
      <br> 
      <br> 
      <select id="mySelect"> 
       <option value="Rome">Rome</option> 
       <option value="London">London</option> 
      </select> 
      <br> 
      <br> 

      <div id="map"></div> 
     </center> 
    </body> 
..... 
+0

請發佈您的相關html代碼 - 您必須設置html元素的大小 – messerbill

回答

1

谷歌地圖,如圖Documentation

var image = { 
     url: 'image.png', 
     // This marker is 20 pixels wide by 32 pixels high. 
     size: new google.maps.Size(20, 32), 
     // The origin for this image is (0, 0). 
     origin: new google.maps.Point(0, 0), 
     // The anchor for this image is the base at (0, 32). 
     anchor: new google.maps.Point(0, 32) 
    }; 

希望這有助於您可以添加圖像的其它特性。

+0

非常感謝! :) –

0

您可以使用select查詢像$('#image-id')選擇的任何元素。 .css()函數用於在特定元素中設置css樣式。傳遞css()函數中的值。

$('#map').css({'height':'50px;','width':'50px;'});map是特定image元素的ID。

$(function() { 
    $('#mySelect').change(function() { 
     var selectedCity = $('#mySelect').val(); 
     var myLatLng = new google.maps.LatLng(eval(selectedCity).lan, eval(selectedCity).lng); 
     venueMap.setCenter(myLatLng); 
     image = 'img.png'; 
     marker = new google.maps.Marker({ 
      position: myLatLng, 
      map: venueMap, 
      icon: image 
     }); 
     marker.setMap(venueMap); 
    }); 


$('#map').css({'height':'50px;','width':'50px;'}); 
}); 
+0

我想更改大小的圖像沒有任何ID。 –

+0

更新,現在試試。 –

+0

該地圖位於'

'元素內,對嗎? –

0

的jQuery,你可以使用道具功能:

$(element).prop('width', 100); 
$(element).prop('height', 50); 

其中$(元素)是圖像標籤選擇器。 prop()的第一個參數是屬性/屬性的名稱,第二個參數是屬性的新值。

對於純JavaScript,你需要選擇的元素,然後更改屬性爲:

document.getElementById('img').width = 100; 
document.getElementById('img').height= 50; 

在哪裏「IMG」是圖像的ID。您也可以使用替代選擇器,如getElementsByClassName或getElementsByName。