简体   繁体   English

jQuery通过ID使用字符串动态选择对象

[英]Jquery select object by id dynamically using a string

I have an image gallery that uses modals. 我有一个使用模态的图片库。 In the image modal or outside it, the user can click on a star and mark the image as favorite. 在图像模态中或外部,用户可以单击星号并将图像标记为收藏。 Favorite images have a class that makes the star change color its to 'golden'. 最喜欢的图像具有使星星将颜色更改为“金色”的类。 If a user marks a favorite in the modal, I need to change the color of the star of that image outside the modal as well. 如果用户在模态中标记了收藏夹,我还需要在模态之外更改该图像的星星颜色。

To do this, I thought I could have a 为此,我认为我可以

<p id = "modal_counter" class = "hidden">number</p>

in the modal, so that I can get the image number. 在模态中,以便我可以获取图像编号。 Then I use that image number as part of a jquery selector. 然后,我将该图像编号用作jquery选择器的一部分。

The images in the gallery have something like this: 画廊中的图像如下所示:

<p id = "image_number" class = "hidden"></p>
<!-- for example: <p id = "image_2"  class = "hidden"></p> -->

So, all I need is to successfully select the hidden p tag next to each image, then I could use jquery's next() or parent().find(). 因此,我需要做的是成功选择每个图像旁边的隐藏p标签,然后可以使用jquery的next()或parent()。find()。

This is the relevant code: 这是相关代码:

...
var modal_counter = div_parent.find('#modal_counter').text();
var counter = "#image_"+String(modal_counter);

/* I would like the counter to be something like "#image_2" */

var $(counter).parent().find('.star').addClass('golden')
...

This doesn't work. 这行不通。 Jquery tries to find an object using this selector: jQuery尝试使用此选择器查找对象:

"#image_\n 2"

and of course, it doesn't find one. 当然,它找不到一个。 I tried removing the \\n with a regex, but it still doesn't work: 我尝试使用正则表达式删除\\ n,但仍然无法正常工作:

counter_processed = modal_counter.replace(/(<([^>]+)>)/ig,"");

It'll probably be better to use a data-attribute on your element rather than getting the text content. 在元素上使用data-attribute可能比获取文本内容更好。 Do: 做:

<p id = "modal_counter" class = "hidden" data-counter="number">number</p>

Then you can: 那么你也能:

...
var modal_counter = div_parent.find('#modal_counter').data('counter');
var counter = "#image_" + modal_counter;

/* I would like the counter to be something like "#image_2" */

var $(counter).parent().find('.star').addClass('golden')
...

This way you don't bind your javascript to the text inside your elements. 这样,您就不会将javascript绑定到元素内部的文本。

jQuery has a handy trim function: jQuery具有方便的trim功能:

var modal_counter = div_parent.find('#modal_counter').text().trim();

Try this from your console: 在控制台上尝试:

$.trim("\n\nHello\r\nThere\n")

See how it removes the leading and trailing white-space? 看看它如何消除开头和结尾的空白?

Let's say you have: 假设您有:

<p id="modal_1" class="hidden">Img 1</p>

you can extract the number both using: 您可以使用以下两种方法提取数字:

// If you need it from the text
var n = $("[id^=modal_]").text().match(/\d+/); // 1

or 要么

// If you need it from the ID
var n = $("[id^=modal_]")[0].id.match(/\d+/);  // 1

Than to target the image like: 比将图片定位为:

<img id="image_1" src="someimage.jpg" alt="SEO">

you do: 你做:

$('#image_'+ n)

If you really just have: 如果您真的只有:

<p id="modal_counter" class="hidden">3</p>

than you can do: 比你能做的

var n = $('#modal_counter').text();  // "3"
$("#image_"+ n)                      // .fadeIn() or whatever...

Just make sure you don't have duplicate modal_counter ID elements on your page. 只要确保您的页面上没有重复的modal_counter ID元素即可。 ID has to be unique. ID必须是唯一的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM