如果不是,则在type =“ text / html”中声明

时间:2018-11-01 03:01:28

标签: javascript jquery object knockout.js embed

如何在type =“ text / html”内使用if else语句

我想对IE浏览器使用<embed>标签,否则对<object>标签使用

如果是Internet Explorer

 <embed data-bind="attr: { src: data.files()[currentPage()] + '#toolbar=0&amp;navpanes=0&amp;scrollbar=0' }" type="application/pdf" style="width: 100%; height: 800px !important;">

其他浏览器

 <object data-bind="attr: { data: data.files()[currentPage()] + '#toolbar=0&amp;navpanes=0&amp;scrollbar=0' }" type="application/pdf" width="100%" height="600px"></object>

1 个答案:

答案 0 :(得分:1)

尝试

创建一个检查其是否为IE的函数

然后使用javascirpt进行追加

function detectIE() {
  var ua = window.navigator.userAgent;

  var msie = ua.indexOf('MSIE ');
  if (msie > 0) {
    return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
  }

  var trident = ua.indexOf('Trident/');
  if (trident > 0) {
    var rv = ua.indexOf('rv:');
    return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
  }

  var edge = ua.indexOf('Edge/');
  if (edge > 0) {
    return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
  }

  return false;
}

console.log(detectIE())

if(detectIE()){
  document.body.innerHTML=`<embed data-bind="attr: { src: data.files()[currentPage()] + '#toolbar=0&amp;navpanes=0&amp;scrollbar=0' }" type="application/pdf" style="width: 100%; height: 800px !important;">`;

}else{

  document.body.innerHTML=`<object data-bind="attr: { data: data.files()[currentPage()] + '#toolbar=0&amp;navpanes=0&amp;scrollbar=0' }" type="application/pdf" width="100%" height="600px"></object>`;

}

https://codepen.io/anon/pen/pxMRxG