使用jQuery创建一个链接

时间:2018-04-10 00:10:18

标签: jquery url hyperlink

我有这个代码,我正在解析一个URL,将路径传递给另一个URL,两者都在同一个域内。我已经成功地解析了一个新的URL,但现在我无法将其转换为链接。这就是我所拥有的:

df[df['b'] == 4]['a'] = 99
$(document).ready(function() {
  var URL = 'https://www.site-name.com/f1'; // MAIN URL

  var otherURL = 'https://www.site-name.com/Manual/module_1_4_2.htm'; // Other URL

  //Create a new link with the url as its href:
  var a = $('<a>', {
    href: otherURL
  });
  var sResult = '<b>Protocol:</b> ' + a.prop('protocol') + '<br/>' + '<b>Host name: </b>' + a.prop('hostname') + '<br/>' + '<b>Path: </b>' + a.prop('pathname') + '<br/>'

  var path = a.prop('pathname'); // pass the path to the MAIN URL 
  var newURL = URL + "?page=" + path.substring(path.lastIndexOf('/') + 1);

  // display new URL 
  $('span').html(sResult + "<br><b>New URL: </b>" + newURL);

  // Correctly displays New URL:https://www.site-name.com/f1? page=module_1_4_2.htm

  // How do I turn it into a link ?

});

2 个答案:

答案 0 :(得分:1)

创建一个新元素,设置链接的网址和文字,然后在ansible-playbook /ansible/provision.yml -e 'ansible_python_interpreter=/usr/bin/python3' -c local

之后追加它

NEW URL:
$(document).ready(function() {
  var URL = 'https://www.site-name.com/f1'; // MAIN URL

  var otherURL = 'https://www.site-name.com/Manual/module_1_4_2.htm'; // Other URL

  //Create a new link with the url as its href:
  var a = $('<a>', {
    href: otherURL
  });
  var sResult = '<b>Protocol:</b> ' + a.prop('protocol') + '<br/>' + '<b>Host name: </b>' + a.prop('hostname') + '<br/>' + '<b>Path: </b>' + a.prop('pathname') + '<br/>'

  var path = a.prop('pathname'); // pass the path to the MAIN URL 
  var newURL = URL + "?page=" + path.substring(path.lastIndexOf('/') + 1);

  // display new URL
  var newLink = $('<a>',{
    href: newURL,
    text: newURL
  })
  $('span').html(sResult + "<br><b>New URL: </b>").append(newLink);
});

答案 1 :(得分:1)

与您创建a的方式相同。

$(document).ready(function() {
  var URL = 'https://www.site-name.com/f1'; // MAIN URL

  var otherURL = 'https://www.site-name.com/Manual/module_1_4_2.htm'; // Other URL

  //Create a new link with the url as its href:
  var a = $('<a>', {
    href: otherURL
  });
  var sResult = '<b>Protocol:</b> ' + a.prop('protocol') + '<br/>' + '<b>Host name: </b>' + a.prop('hostname') + '<br/>' + '<b>Path: </b>' + a.prop('pathname') + '<br/>'

  var path = a.prop('pathname'); // pass the path to the MAIN URL 
  var newURL = URL + "?page=" + path.substring(path.lastIndexOf('/') + 1);

  // display new URL 
  $('span').html(sResult + "<br><b>New URL: </b>" + newURL);

  // Correctly displays New URL:https://www.site-name.com/f1? page=module_1_4_2.htm

  // How do I turn it into a link ?

  var new_a = $('<a>', {
    href: newURL,
    text: 'Click here for new URL'
  });
  $('div').append(new_a);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span></span>
<div></div>