简体   繁体   English

如何定位html 5属性“data-src”并用jQuery替换为“src”?

[英]How to target html 5 attribute “data-src” and replace with “src” with jQuery?

I'm trying to find a "best" method for dynamically loading files, and other elements by using the new html 5 "data-" attribute for storage and then removing it when needed. 我正在尝试通过使用新的html 5“data-”属性进行存储,然后在需要时将其删除,从而找到动态加载文件和其他元素的“最佳”方法。

My Question: 我的问题:

How do I target the "data-" attribute and remove it thus leaving the remaining attribute name? 如何定位“data-”属性并将其删除,从而留下剩余的属性名称?

For example, this: 例如,这个:

<!-- Custom CSS -->
<link rel="stylesheet" class="custom" data-href="css/mobile.css">

Would turn into this: 会变成这样:

<!-- Custom CSS -->
<link rel="stylesheet" class="custom" href="css/mobile.css">



Edit: This entire experiment is for Responsive Design (ui/performance), I need to be able to load css files/ elements depending on device resolution. 编辑:整个实验是针对响应式设计(ui / performance),我需要能够根据设备分辨率加载css文件/元素。 This will result in higher performance for smaller devices to prevent unnecessary data loading. 这将为较小的设备带来更高的性能,以防止不必要的数据加载。 I'm using enquire.js plugin (amazing btw), which allows you to have functions fire off when a media query is targeted. 我正在使用enquire.js插件 (惊人的顺便说一句),它允许您在定位媒体查询时触发功能。 So in an effort to stay current I wanted to use the "data-" attribute to add/remove elements from the DOM depending on device resolution. 因此,为了保持最新状态,我想使用“data-”属性根据设备分辨率添加/删除DOM中的元素。

This tutorial by Christian Heilmann does just this: http://christianheilmann.com/2012/12/19/conditional-loading-of-resources-with-mediaqueries/ Christian Heilmann的这篇教程就是这样做的: http//christianheilmann.com/2012/12/19/conditional-loading-of-resources-with-mediaqueries/

I'm trying to combine his technique with enquire.js 我正在尝试将他的技术与enquire.js结合起来

Assuming you know how to get to your elements, enabling is a one liner: 假设你知道如何获得你的元素,启用是一个单行:

$(element).attr("href", $(element).data("href"));

since the data-href does nothing, you don't need to remove it. 由于data-href什么也不做,你不需要删除它。 Just mirror it as real href attribute. 只需将其镜像为真正的href属性。 To invalidate, either invalidate the href attribute again: 要使其无效,请再次使href属性无效:

$(element).attr("href", false);

or remove it altogether: 或者完全删除它:

$(element).removeAttr("href");

And for <img> it's the same trick but with the string "src" instead of "href" 并且对于<img>它是相同的技巧,但使用字符串"src"而不是"href"

While I agree that it's worth questioning whether this is the correct way to approach this problem, from a technical stand-point this is how you access, remove, and add attributes on elements. 虽然我同意值得质疑这是否是解决此问题的正确方法,但从技术角度来看,这是您访问,删除和添加元素属性的方式。

$('[data-attrName]').each(function() {
    var $el = $(this);
    var val = $el.attr('data-attrName');
    $el.removeAttr('data-attrName')
       .attr('attrName', val);
});

To replace all data-attributename attributes with attributename can be accomplished simply by looping over the data object. 要使用attributename替换所有data-attributename属性,只需循环遍历数据对象即可。

$(element).each(function() {
    var 
        el = $(this),
        data = el.data();

        $.each(data,function(key,value) { el.attr(key,value); });
});

I'll point out what others have already pointed out, though - I think you have decided that this is what you need to do to accomplish something, when it probably isn't what you should be doing. 我会指出其他人已经指出的内容 - 我认为你已经决定这是你需要做的事情来完成某些事情,而这可能不是你应该做的事情。

Try asking a question whose answer will solve your actual problem, not asking a question whose answer will solve the problem that you are experiencing when you are using it to try and solve another problem....I'll understand if you don't understand that last sentence. 尝试提出一个问题,其答案将解决您的实际问题,而不是问一个问题,其答案将解决您在使用它来尝试解决另一个问题时遇到的问题....我会理解,如果您不这样做明白最后一句话。

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

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