用jQuery替换图像链接

时间:2015-11-25 09:34:34

标签: javascript jquery html

我在一个元素中有一个图像,其中的灯箱图像在悬停时显示。我想用灯箱图像替换元素中的图像。我的内容是动态匹配的,我无法更改html。

我的所有图片网址都以/ 250/250结尾 - 而某些lighbox图片以/ 400/300结尾 - 我使用下面的脚本替换它们:

$('.new-structure .image a img').each(function () {
var src = $(this).attr('src');
$(this).attr('src', src.replace("/250/250", "/400/300"));
});

但是一些灯箱图片有另一个结尾,我不能让脚本工作。他们看起来像这样:

/img/0~BC6F034B-3404-4FBA-B26A-9B2990552E72~400~300~1

如何将图像与/ 250/250的结尾匹配,以及以400~300~1结尾的图像?

更新:主要问题:当我用300/200替换250/250时,我不需要更改完整链接 - 除了结尾之外它们是相同的 - 当更换~400~300~1图像时我需要交换完整的链接,因为它不同。

我需要替换它:

 https://mywebsite.com/imgs/2d873287-7eb5-497a-a813-aef655acdb74/250/250

用这个:

 https://mywebsite.com/img/0~3B07CED6-08FF-47CD-9D25-D908774F728D~400~300~1

基于url的结尾影响图像大小:/ 250/250和~400~300~1

更新:意识到根据元素类交换它们可能更容易..

灯箱图片如下所示:

<a class="mainColorbox" href="/img/0~676B9DBB-5D93-4481-B241-74B619F96188~400~300~1"></a>

目标图片:

<div class="mainPicture">
<img src="/imgs/7fb6d7bd-b9e7-44b2-a533-a485b93456ac/250/250" class="photo">

1 个答案:

答案 0 :(得分:1)

使用正则表达式代码替换字符串:

private static EntityCollection GetConnectionList(IOrganizationService service, EntityReference childCase)
    {
        EntityCollection eColl = service.RetrieveMultiple(new FetchExpression("<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
               "<entity name='incident'>" +
               "<attribute name='incidentid' />" +
               "<filter type='and'>" +
               "<condition attribute='statecode' operator='eq' value='0' />" +
               "<condition attribute='incidentid' operator='eq' value='" + childCase.Id + "' />" +
               "</filter>" +
               "<link-entity name ='connection' from='record1id' to='incidentid'> "+
               "<filter type='and'>" +
               "<condition attribute='record2id' operator='not-null' />" +
               "<condition attribute='record2roleid' operator='eq' uiname='Witness' uitype='connectionrole' value={16DA474E-EC82-E511-8100-3863BB35FDE0} />" +
               "<condition attribute='statecode' operator='eq' value='0' /> " +
               "</filter>" +
               "</link-entity>" +
               "</entity>" +
               "</fetch>"));
        return eColl;
    }

//calling above methods in main method...
// Get the connection records from the child cases
EntityCollection connectionColl = GetConnectionList(service, childCaseRef);

// Create copy of connection to link FTP prosecution case
CreateCloneOfConnections(connectionColl, service, childCaseRef, parentCaseRef, context);

private static void CreateCloneOfConnections(EntityCollection connectionColl, IOrganizationService service, EntityReference childCaseRef, EntityReference parentCaseRef, IPluginExecutionContext context)
    {
        foreach (Entity connection in connectionColl.Entities)
        {
            Entity newEntity = Utility.CloneEntity(connection);

            // remove primary key reference as we do not want to duplicate the record 
            if (newEntity.Contains("connectionid"))
                newEntity.Attributes.Remove("connectionid");

            if (newEntity.Contains("new_incidentidid"))
            {
                newEntity.Attributes.Remove("new_incidentidid");
                newEntity.Attributes.Add("new_incidentidid", parentCaseRef);
            }

            // remove child case reference if it is there 
            if (newEntity.Contains("new_childcase"))
                newEntity.Attributes.Remove("new_childcase");

            // Get the token number of parent case
            Entity caseEntity = service.Retrieve(context.PrimaryEntityName, childCaseRef.Id, new ColumnSet(new[] { "ticketnumber" }));
            if (caseEntity != null)
            {
                newEntity.Attributes.Add("new_childcase", caseEntity.GetAttributeValue<string>("ticketnumber"));
            }

            Guid id = service.Create(newEntity);

            //// associate the child case consideration to parent case
            //AssociateConnections(service, new EntityReference("connection", id), parentCaseRef);
        }
    }

否则斜杠将按照您的示例进行编码。

在此处了解详情:How to globally replace a forward slash in a JavaScript string?