2016-08-18 52 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。