简体   繁体   English

javascript函数转换为等效的jquery

[英]javascript function convert into jquery equivalent

I have a javascript function written as follows which works just fine in Firefox for downloading given txt content. 我有一个如下编写的javascript函数,该函数在Firefox中可以很好地用于下载给定的txt内容。

  self.downloadURL = function (url) {
        var iframe;
        iframe = document.getElementById("hiddenDownloader");        
        if (iframe === null) {
            iframe = document.createElement('iframe');
            iframe.id = "hiddenDownloader";
            iframe.style.display = "none";
            document.body.appendChild(iframe);
        }
        iframe.src = url;
    }

but it does n't work fine with IE 9 due to some reasons. 但由于某些原因,它在IE 9中无法正常工作。 So i tried to convert into equivalent jquery because jquery has compatibility with all the browsers. 所以我尝试转换成等效的jquery,因为jquery具有与所有浏览器的兼容性。

here is the jquery equivalent of the same function: 这是相同功能的jQuery等效项:

   self.downloadURL = function (url) {
        var iframe;
        iframe = $("#hiddenDownloader");        
        if (iframe === null) {
            iframe = $('<iframe></iframe>');
            iframe.id = "hiddenDownloader";            
            $("#hiddenDownloader").css("display", "none");
            $(document.body).append(iframe);
        }
        iframe.src = url;
    }

But now it does n't work in both the browsers. 但是现在,这两种浏览器都无法使用。 Please help letting me know what am i doing wrong. 请帮助让我知道我在做什么错。

Your problem is in: 您的问题出在:

iframe = $('<iframe></iframe>');
iframe.id = "hiddenDownloader";       

iframe refers to a jQuery Object not a DOM Node. iframe是指jQuery对象而不是DOM节点。 You will have to use .prop to set the id: 您将必须使用.prop来设置ID:

iframe = $('<iframe></iframe>');
iframe.prop('id', "hiddenDownloader");       

And also you have the same problem here: 而且你在这里也有同样的问题:

if (iframe === null) {

Where you will need the check the length of iframe : 在需要检查iframe长度的地方:

if (iframe.length === 0) {

And again at iframe.src = url; 再次在iframe.src = url; Maybe you can figure this one out: 也许您可以弄清楚这一点:

iframe.attr('src', url);

But why would you convert vanilla JavaScript to jQuery? 但是,为什么要将原始JavaScript转换为jQuery?

Seems like a strange way to download a file. 似乎是一种奇怪的下载文件的方式。

In any case, this probably works the same was as the original: 无论如何,这可能与原始版本相同:

if (!iframe.get(0)) {
  ...
}

... and the id property as another poster mentions. ...以及另一个海报提到的id属性。

尝试:

iframe = $("#hiddenDownloader")[0];
  • Replace iframe.src = url; 替换iframe.src = url; with iframe.attr("src",url + "?"+ Math.random()); 使用iframe.attr("src",url + "?"+ Math.random()); to prevent caching issue in IE9 . 防止IE9中的缓存问题
  • Replace iframe.id = "hiddenDownloader"; 替换iframe.id = "hiddenDownloader"; with iframe.attr("id","hiddenDownloader"); 使用iframe.attr("id","hiddenDownloader");
  • Replace if (iframe === null) { with if (iframe.length === 0) { if (iframe === null) {替换为if (iframe.length === 0) {
iframe = document.getElementById("hiddenDownloader");          
iframe = $("#hiddenDownloader");        

These lines aren't equivalent. 这些行不相等。 The second one create a jQuery object, so the check for null will never equate to true 第二个创建一个jQuery对象,因此对null的检查永远不会等于true

This link may help u: 此链接可以帮助您:

http://www.sitepoint.com/forums/showthread.php?743000-IE9-Iframes-DOCTYPES-and-You http://www.sitepoint.com/forums/showthread.php?743000-IE9-Iframes-DOCTYPES-and-You

or u can add this to the top: 或者您可以将其添加到顶部:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

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

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