将值传递到 If() 循环

发布于 2024-12-11 09:07:21 字数 2139 浏览 0 评论 0 原文

我正在使用一个 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() 循环内使用该变量,因为我需要为我拥有的每个数据点创建一个唯一的变量。数据点的数量根据检索的结果而变化,并且我没有尝试创建动态命名的变量,例如 marker1marker2

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别在捏我脸啦 2024-12-18 09:07:21

如果您想使用 jQuery,您可以向包含“lat”和“lng”值的 img 元素添加属性。

<img src="path/image.png" data-coordinates="{lat:42,lng:-71}">

然后,使用 jQuery:

//pass your image selector, just using 'img' for this example's sake
var coordinates = $('img').data("coordinates");
// coordinates.lat = 42
// coordinates.lng = -71

如果您不想使用 jquery,您可以将“lat”和“long”值添加为 img 元素上的单独数据属性,然后使用 javascript 的 getAttribute 函数来获取这些值。

<img src="path/image.png" data-lat="41" data-lng="-71">

然后,使用 javascript:

e.imageTarget.getAttribute("data-lat") // '42'
e.imageTarget.getAttribute("data-lng") // '-71'

此外,您可能会遇到 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.

<img src="path/image.png" data-coordinates="{lat:42,lng:-71}">

Then, with jQuery:

//pass your image selector, just using 'img' for this example's sake
var coordinates = $('img').data("coordinates");
// coordinates.lat = 42
// coordinates.lng = -71

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.

<img src="path/image.png" data-lat="41" data-lng="-71">

Then, with javascript:

e.imageTarget.getAttribute("data-lat") // '42'
e.imageTarget.getAttribute("data-lng") // '-71'

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().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文