Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
511 views
in Technique[技术] by (71.8m points)

javascript - 如何创建与输入文本匹配的搜索栏,并且仅显示在其属性中具有该文本的图像(href)? jQuery查询(How to create search bar that matches the input text and displays only images(href) that has that text in their attributes ??? JQuery)

Trying to create a search bar ... when user writes text in the input field i want to display only those images(their href) that contain that text in their caption(they all have captions and those captions are added as attributes to them)... here is the code... the lightbox.option is just a plugin for the pictures(尝试创建搜索栏...当用户在输入字段中写入文本时,我只想显示在标题中包含该文本的图像(它们的href)(它们都有标题,并且这些标题作为属性添加到它们中) ...这是代码... lightbox.option只是图片的插件)

const mainDiv = $('<div></div>').addClass('wrapper'); mainDiv.appendTo('body'); const input = $('<input>').attr({'id':'searchInput','type':'text','placeholder':'Search(16pt)'}); input.appendTo(mainDiv); const imgsMainWrap = $('<div></div>').addClass('imgsWrpaer'); imgsMainWrap.appendTo(mainDiv); const captions = [ '', 'I love hay bales. Took this snap on a drive through the countryside past some straw fields.', 'The lake was so calm today. We had a great view of the snow on the mountains from here.', 'I hiked to the top of the mountain and got this picture of the canyon and trees below', 'It was amazing to see an iceberg up close, it was so cold but didn’t snow today.', 'The red cliffs were beautiful. It was really hot in the desert but we did a lot of walking through the canyons.', 'Fall is coming, I love when the leaves on the trees start to change color.', 'I drove past this plantation yesterday, everything is so green!', 'My summer vacation to the Oregon Coast. I love the sandy dunes!', 'We enjoyed a quiet stroll down this countryside lane.', 'Sunset at the coast! The sky turned a lovely shade of orange.', 'I did a tour of a cave today and the view of the landscape below was breathtaking.', 'I walked through this meadow of bluebells and got a good view of the snow on the mountain before the fog came in.' ] for(let i =1; i <= 12; i++){ const innerImgWrapper = $('<div></div>').addClass('innerImgWrapper'); $(innerImgWrapper).appendTo(imgsMainWrap); const links = $('<a></a>').attr({'href':'photos/0' + [i] + '.jpg','data-lightbox':'images', 'data-title': captions[i]}); if(i > 9){ links.attr('href','photos/' + [i] + '.jpg'); } $(links).appendTo(innerImgWrapper); const img = $('<img>').attr({'src':'photos/0'+[i]+'.jpg'}); if(i > 9){ img.attr({'src':'photos/'+[i]+'.jpg'}); } $(img).appendTo(links); input.on('keyup', function(){ const inputValue = $(input).val().toLowerCase(); const captions = $(links).attr('data-title'); if(captions.indexOf(inputValue) != -1){ ??????????? } // } }); } // Next comes the fun part. Start trying to find ways to check if the current value of the // search input is included within any of the captions, and if so, log the associated image // to the console. lightbox.option();   ask by hristijan56 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use data-attribute selector * which looks for matching characters anywhere in data-* attribute.(您可以使用数据属性选择器*来查找data- *属性中任何位置的匹配字符。)

Here is a list of other data selector type you can use ( source ):(以下是您可以使用的其他数据选择器类型的列表( ):) [data-value] { /* Attribute exists */ } [data-value="foo"] { /* Attribute has this exact value */ } [data-value*="foo"] { /* Attribute value contains this value somewhere in it */ } [data-value~="foo"] { /* Attribute has this value in a space-separated list somewhere */ } [data-value^="foo"] { /* Attribute value starts with this */ } [data-value|="foo"] { /* Attribute value starts with this in a dash-separated list */ } [data-value$="foo"] { /* Attribute value ends with this */ } Also, you need to take the input keyup event outside the loop so it is not attached multiple times to the same element.(另外,您需要将输入keyup事件带到循环之外,这样它就不会多次附加到同一元素上。) The logic is to hide elements on search using .hide() then show the elements filtered with the data selector using .show() .(所述逻辑,以在搜索隐藏元件使用.hide()然后显示使用与所述数据选择器过滤元件.show()) See this example:(请参阅以下示例:) const mainDiv = $('<div></div>').addClass('wrapper'); mainDiv.appendTo('body'); const input = $('<input>').attr({ 'id': 'searchInput', 'type': 'text', 'placeholder': 'Search(16pt)' }); input.appendTo(mainDiv); const imgsMainWrap = $('<div></div>').addClass('imgsWrpaer'); imgsMainWrap.appendTo(mainDiv); const captions = [ '', 'I love hay bales. Took this snap on a drive through the countryside past some straw fields.', 'The lake was so calm today. We had a great view of the snow on the mountains from here.', 'I hiked to the top of the mountain and got this picture of the canyon and trees below', 'It was amazing to see an iceberg up close, it was so cold but didn't snow today.', 'The red cliffs were beautiful. It was really hot in the desert but we did a lot of walking through the canyons.', 'Fall is coming, I love when the leaves on the trees start to change color.', 'I drove past this plantation yesterday, everything is so green!', 'My summer vacation to the Oregon Coast. I love the sandy dunes!', 'We enjoyed a quiet stroll down this countryside lane.', 'Sunset at the coast! The sky turned a lovely shade of orange.', 'I did a tour of a cave today and the view of the landscape below was breathtaking.', 'I walked through this meadow of bluebells and got a good view of the snow on the mountain before the fog came in.' ] input.on('keyup', function() { const inputValue = $(input).val().toLowerCase(); imgsMainWrap.children().hide(); const links = imgsMainWrap.find("a").filter("[data-title*='" + inputValue + "']"); if (links.length > 0) { links.parent().show(); } // } }); for (let i = 1; i <= 12; i++) { const innerImgWrapper = $('<div></div>').addClass('innerImgWrapper'); $(innerImgWrapper).appendTo(imgsMainWrap); const links = $('<a></a>').attr({ 'href': 'photos/0' + [i] + '.jpg', 'data-lightbox': 'images', 'data-title': captions[i] }); if (i > 9) { links.attr('href', 'photos/' + [i] + '.jpg'); } $(links).appendTo(innerImgWrapper); const img = $('<img>').attr({ 'src': 'photos/0' + [i] + '.jpg' }); if (i > 9) { img.attr({ 'src': 'photos/' + [i] + '.jpg' }); } $(img).appendTo(links); } // Next comes the fun part. Start trying to find ways to check if the current value of the // search input is included within any of the captions, and if so, log the associated image // to the console. //lightbox.option(); <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...