简体   繁体   English

Javascript Sweet Alert 和文本内的 html 链接

[英]Javascript Sweet Alert and html link inside text

i have the following SweetAlert Code..我有以下 SweetAlert 代码..

<script type="text/javascript" charset="utf-8">
    $('.patient-details').click(function(e) {
        e.preventDefault();
        var name = $(this).attr('data-name');
        var gender = $(this).attr('data-gender');
        var age = $(this).attr('data-age');
        var country = $(this).attr('data-country');
        var state = $(this).attr('data-state');
        var address = $(this).attr('data-address');
        var report = $(this).attr('data-report');
        swal({
            title: name,
            text: "Gender: " + gender +"\n" + "Age: " + age +"\n" + "Country: " + country +"\n" + "State: " + state +"\n" + "Address: " + address +"\n" + "Report: " + report,
            confirmButtonColor: "#00B4B4",
            imageUrl: "images/avatar/user.png",
        });
    });
</script>

The var report is a link and i need the link displayed in the modal. var 报告是一个链接,我需要在模态中显示该链接。 I tried html: true etc. html is no longer used.我试过 html: true 等 html 不再使用。 Instead use the content object.而是使用内容对象。 as doc says:正如医生所说:

https://sweetalert.js.org/docs/#content https://sweetalert.js.org/docs/#content

https://sweetalert.js.org/guides/ https://sweetalert.js.org/guides/

But i as a newbie is unable to make sense out of it.但我作为一个新手无法理解它。

Requesting help on how to display the link in the modal and the link to be opened in new window.请求有关如何在模态中显示链接以及在新窗口中打开的链接的帮助。

Update: Since the solutions provided were not working i used another approach using html to resolve it.更新:由于提供的解决方案不起作用,我使用了另一种使用 html 的方法来解决它。 Need to remove text, else text will be default.需要删除文本,否则文本将是默认的。 Codepen link: https://codepen.io/pamela123/pen/GOJZgo Codepen 链接: https ://codepen.io/pamela123/pen/GOJZgo

As the doc says, html is deprecated and no longer works.正如文档所说,不推荐使用html并且不再有效。

They have replaced html with content , which is not a string any longer, but an Object.他们用content替换了html ,它不再是一个字符串,而是一个对象。

This content object looks like this :content对象如下所示:

content: {
    element: "input",
    attributes: {
      placeholder: "Type your password",
      type: "password",
    }
  }

So I guess you can build your own link like this :所以我想你可以像这样建立自己的链接:

content: {
    element: "a",
    attributes: {
      href : report
    }
  }

...and then simply pass the content object to swal : ...然后简单地将content对象传递给swal

swal({
 content: {
        element: "a",
        attributes: {
          href : report
        }
 }
})

Note that this is untested, I'm not sure if element:"a" works.请注意,这是未经测试的,我不确定element:"a"有效。 But anyway, the doc gives a better way :但无论如何,文档提供了更好的方法:

var slider = document.createElement("input");
slider.type = "range";

swal({
  content: slider
});

So you can create a link this way :因此,您可以通过以下方式创建链接:

var link = document.createElement("a");
link.href= report;

swal({
  content: link
});

As an aside, you can heavily optimize the code you provided in your question by caching $(this) (which is expensive to create) and reuse it.顺便说一句,您可以通过缓存$(this) (创建起来很昂贵)并重用它来大量优化您在问题中提供的代码。 Also, .attr("data-x") has a shorthand, .data("x") .此外, .attr("data-x")有一个简写, .data("x")

var $this = $(this)
var name = $this.data('name');
var gender = $this.data('gender');
var age = $this.data('age');
var country = $this.data('country');
var state = $this.data('state');
var address = $this.data('address');
var report = $this.data('report');

OR even better :或者更好:

var attributes = $(this).data()

which gives an object containing all your data attributes.它给出了一个包含所有数据属性的对象。 Which you can then reach using :然后你可以使用:

text: "Gender: " + attributes['gender'] +"\n" + "Age: " + attributes['age'] +"\n" + "Country: " + attributes['country'] +"\n" + "State: " + attributes['state'] +"\n" + "Address: " + attributes['address'] +"\n" + "Report: " + attributes['report']

Or in ES6 :)或者在 ES6 中 :)

text: `Gender: ${attributes['gender']}\n
        Age: ${attributes['age']}\n
        Country: ${attributes['country']}\n
        State: ${attributes['state']}\n
        Address: ${attributes['address']}\n
        Report: ${attributes['report']}`

Why not try the following (I have never used sweet alert, but after reading the documentation this is what I would try)为什么不尝试以下(我从未使用过甜蜜警报,但在阅读文档后,这是我会尝试的)

var link= document.createElement("a");
link.href =  report  // or link.setAttribute("href", report)


swal({
title: name,
text: "Gender: " + gender +"\n" + "Age: " + age +"\n" + "Country: " + country +"\n" + "State: " + state +"\n" + "Address: " + address +"\n" + "Report: " + report,
confirmButtonColor: "#00B4B4",
imageUrl: "images/avatar/user.png",
content:link
});
});

Or要么

swal({
    title: name,
    text: "Gender: " + gender +"\n" + "Age: " + age +"\n" + "Country: " + country +"\n" + "State: " + state +"\n" + "Address: " + address +"\n" + "Report: " + report,
    confirmButtonColor: "#00B4B4",
    imageUrl: "images/avatar/user.png",
    content:{
      element:"a",
      attributes:{
        href:report
      }
     }
    });
    });

hope that helps希望有帮助

As Jeremy Thille found out in his commentary on Oct. 31 '17 at 10:36:正如 Jeremy Thille 在 17 年 10 月 31 日 10:36 的评论中发现的:

You do not need to use the option "content" for a simple link in the text.对于文本中的简单链接,您不需要使用“内容”选项。 The option "text" can only display pure text, no html.选项“text”只能显示纯文本,不能显示html。 However, the option "html" can display html.但是,选项“html”可以显示 html。

Not to be confused with the old version SweetAlert 1.X: There you had to set html = true.不要与旧版本 SweetAlert 1.X 混淆:在那里你必须设置 html = true。

In SeewtAlert2, the html is set directly in the "html" option.在 SeewtAlert2 中,html 直接在“html”选项中设置。 Do not use option "text" in this case.在这种情况下不要使用选项“文本”。

Works fine in sweetAlert.version = '6.9.1';在 sweetAlert.version = '6.9.1' 中工作正常;

Example of Jeremy Thille:杰里米·蒂尔的例子:

$('.patient-details').click(function(e) {
  e.preventDefault();
  var $this = $(this)
  var name = $this.data('name');
  var gender = $this.data('gender');
  var age = $this.data('age');
  var country = $this.data('country');
  var address = $this.data('address');
  var report = $this.data('report');

  swal({
  title: name,
  html:
      "Gender: " + gender +"<br>" +
     "Age: " + age +"<br>" +
     "Country: " + country +"<br>" +
     "Address: " + address +"<br>" +
     "Report: " + report +"<br>" +
      "<a href='report'>Report</a> " +
     "and other HTML tags"
  });
});

https://codepen.io/jeremythille/pen/wPazMw https://codepen.io/jeremythille/pen/wPazMw

Found this answer here , all credits to Tristan Edwards在这里找到了这个答案,所有功劳都归功于特里斯坦爱德华兹

const el = document.createElement('div')
el.innerHTML = "Here's a <a href='http://google.com'>link</a>"

swal({
  title: "Hello!",
  content: el,
})

If you are not still able to find a solution, I tried recreating the modal https://codepen.io/AJALACOMFORT/pen/zPGqNe?editors=0010如果您仍然无法找到解决方案,我尝试重新创建模态https://codepen.io/AJALACOMFORT/pen/zPGqNe?editors=0010

window.onload= function(){
  var that = $(".patient-details")
  var name = $(that).attr('data-name');
var gender = $(that).attr('data-gender');
var age = $(that).attr('data-age');
var country = $(that).attr('data-country');
var address = $(that).attr('data-address');
var report = $(that).attr('data-report');

   //creates h2
    var h2 = document.createElement("h2")
        //adds text
        h2.innerHTML = name
  //creates p tag 
    var p = document.createElement("p")
         p.innerHTML =  "Gender: " + gender +"\n" + "Age: " + age +"\n" + "Country: " + country +"\n"  + "Address: " + address +"\n" + "Report: " + report
  //creates button
  var button = document.createElement("button")
    //adds the onclick function
      button.setAttribute("onclick", "callbutton(event)")
  button.innerHTML ="ok"
  button.className += "confirm"
  var link = document.createElement("a")
      link.setAttribute("href", report)
      link.innerHTML ="Link"
      link.className += "report-link"
  //appends all the elements into the mymodal div
  var modal = document.getElementById("modal-inner")
        modal.appendChild(h2)
          modal.appendChild(p)
          modal.appendChild(link)
        modal.appendChild(button)


}
 //listens to click event of the checkbox
  function callbutton(){
      document.getElementById("checkboxabove").checked = false;
  }

Hopefully it helps.希望它有帮助。 (Note I did not use the same transition effect like in sweet alert, so adjust as you please) (注意我没有使用与甜蜜警报中相同的过渡效果,所以请随意调整)

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

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