我正在使用一个 jQuery 插件,它有一个事件处理程序,其返回函数为我提供 HTMLImageElement 对象作为事件属性 e.imageTarget
。此 e.imageTarget
的来源来自特定
内的一堆
。
问题:我想将一对数字 42.345573, -71.098326
传递到下面所示代码中的 if()
循环中。我该怎么做?
我失败的尝试:
创建一个包含 2 个值的
,并将其放置在被抓取的
之后。然后在
if()
循环中,在
上使用 jQuery 的
.next()
来选择
< div>
。但是我的 jquery 选择代码似乎不起作用!我使用
console.log(e.imageTarget.previous());
尝试了此操作,并收到错误
Uncaught TypeError: Object #;没有方法“previous”
。可以使用 jQuery DOM 横向选择器吗?
从以下位置抓取大量图像:
<img src="http://www.google.com/images/experiments/nav_logo78.png">
<img src="http://www.google.com/images/experiments/nav_logo78.png">
<img src="http://www.google.com/images/experiments/nav_logo78.png">
<div id="lat">42.345573</div>
<div id="lng">-71.098326</div>
e.imageTarget:
代码:
Galleria.ready(function(options) {
// Load Streetview after image related to Streetview has finished loading
this.bind("loadfinish", function(e) {
if(e.imageTarget.src.search('png') != -1) {
console.log(e.imageTarget);
// Create Street View
var fenway = new google.maps.LatLng(42.345573,-71.098326);
var panoOptions = {
position: fenway
};
var panorama = new google.maps.StreetViewPanorama(document.getElementById("streetview"), panoOptions);
}
});
});
其他信息
我不能简单地创建一个变量,将其设置为我想要的值并在 if() 循环内使用该变量,因为我需要为我拥有的每个数据点创建一个唯一的变量。数据点的数量根据检索的结果而变化,并且我没有尝试创建动态命名的变量,例如 marker1
、marker2
等
I am using a jQuery plugin that has an event handler whose return function gives me the HTMLImageElement object as an event property e.imageTarget
. The source of this e.imageTarget
is from a bunch of <img>
inside a particular <div>
.
Problem: I want to pass a pair of numbers 42.345573, -71.098326
into the if()
loop in the code shown below. How can I do this?
My failed attemps:
Create a <div>
containing the 2 values, and place it after the <img>
being grabbed. Then from within the if()
loop, use jQuery's .next()
on the <img>
to select the <div>
. However my jquery selection code doesnt seem to work! I tried this using console.log(e.imageTarget.previous());
and got the error Uncaught TypeError: Object #<HTMLImageElement> has no method 'previous'
. Is using jQuery DOM transversal selectors possible?
Bunch of Imgs being grabbed from:
<img src="http://www.google.com/images/experiments/nav_logo78.png">
<img src="http://www.google.com/images/experiments/nav_logo78.png">
<img src="http://www.google.com/images/experiments/nav_logo78.png">
<div id="lat">42.345573</div>
<div id="lng">-71.098326</div>
e.imageTarget:
<img style="display: block; width: 170px; height: 323px; position: absolute; top: 0px; left: 130px; opacity: 1; " src="http://www.google.com/images/experiments/nav_logo78.png" width="170" height="323">
Code:
Galleria.ready(function(options) {
// Load Streetview after image related to Streetview has finished loading
this.bind("loadfinish", function(e) {
if(e.imageTarget.src.search('png') != -1) {
console.log(e.imageTarget);
// Create Street View
var fenway = new google.maps.LatLng(42.345573,-71.098326);
var panoOptions = {
position: fenway
};
var panorama = new google.maps.StreetViewPanorama(document.getElementById("streetview"), panoOptions);
}
});
});
Additional Info
I cannot simply create a variable, set it to the values i want and use the variable inside the if()
loop because I will need to create a unique variable for each datapoint that I have. The number of datapoints changes depending on the results retrieved, and I did not attempt to create dynamically named variables like marker1
, marker2
etc
发布评论
评论(1)
如果您想使用 jQuery,您可以向包含“lat”和“lng”值的 img 元素添加属性。
然后,使用 jQuery:
如果您不想使用 jquery,您可以将“lat”和“long”值添加为 img 元素上的单独数据属性,然后使用 javascript 的 getAttribute 函数来获取这些值。
然后,使用 javascript:
此外,您可能会遇到
e.imageTarget.previous()
错误,因为它不是 jQuery 对象。您需要执行$(e.imageTarget).previous()
。If you want to use jQuery, you can add attributes to the img element containing the 'lat' and 'lng' values.
Then, with jQuery:
If you dont want to use jquery, you could add the 'lat' and 'long values as individual data attributes on the img element, then use javascript's getAttribute function to get the values.
Then, with javascript:
Additionally, you're probably getting an error with
e.imageTarget.previous()
because it isn't a jQuery object. You would need to do$(e.imageTarget).previous()
.