简体   繁体   English

如果在其中添加空格,为什么不能正常工作?

[英]Why isn't this working If I add spaces to it?

So I'm making a theme for a blogging platform and I have only template editing access to it(means I cannot control the content coming from the server).I want to remove duplicate I want to remove duplicate images from the blog post page.Here is the code 因此,我正在为博客平台创建主题,并且我只能对其进行模板编辑访问(意味着我无法控制来自服务器的内容)。我想删除重复项,我想从博客帖子页面中删除重复的图像。这是代码

it is working fine if the alt tag has no spaces 如果alt标签没有空格,则工作正常

<p>Alt with Spaces.Works fine!</p>

http://jsfiddle.net/mixin/mB7mz/2/ http://jsfiddle.net/mixin/mB7mz/2/

Doens't work if the alt contains spaces http://jsfiddle.net/mixin/G27fk/ 如果alt包含空格http://jsfiddle.net/mixin/G27fk/则不起作用

What's the problem here? 这是什么问题

chnage single quote of alt property to double 将alt属性的单引号更改为双精度

 try following 

function postHeaderImage(){
            headerImage = $('#post-header img').attr('alt');

            $('#post-content img').each(function() {
                postContentImage = $(this).attr('alt');

                if(postContentImage == headerImage){
                    $("#post-content img[alt='"+ postContentImage+"']").remove();
                }
            });
        }

Your selector points to a different id. 您的选择器指向另一个ID。

http://jsfiddle.net/G27fk/4/ http://jsfiddle.net/G27fk/4/

Try this one: 试试这个:

$('#post-content img').each(function() {

The problem was the way you removed the duplicate images, there is a more elegant way to do so. 问题在于删除重复图像的方式,这是一种更优雅的方式。 I updated your JSFiddle to a working example: http://jsfiddle.net/mB7mz/3/ 我将您的JSFiddle更新为一个工作示例: http : //jsfiddle.net/mB7mz/3/

$(document).ready(function(){

    function postHeaderImage(){
        headerImage = $('#post-header img').attr('alt');
        $('#post-content img').each(function() {
            postContentImage = $(this).attr('alt');
            if(postContentImage == headerImage){
                $(this).remove();
            }
        });
    }

    postHeaderImage();

});

There are 2 problems. 有两个问题。

  • One the id used in the second code is mismatched 第一个在第二个代码中使用的ID不匹配
  • Attribute selector is case sensitive alt & Alt in the value 属性选择器的值区分大小写altAlt

So 所以

$(document).ready(function () {

    function postHeaderImage() {
        var headerImage = $('#header-image img').attr('alt');
        $('#post-content img[alt="' + headerImage + '"]').remove();
    }

    postHeaderImage();

});

Demo: Fiddle 演示: 小提琴


If you want case insensitive search then 如果要区分大小写的搜索,则

$(document).ready(function () {
    function postHeaderImage() {
        var headerImage = $('#header-image img').attr('alt').toLowerCase();
        $('#post-content img[alt]').filter(function () {
            return $(this).attr('alt').toLowerCase() == headerImage;
        }).remove();
    }

    postHeaderImage();
});

Demo: Fiddle 演示: 小提琴

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

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