我需要帮助构建一个锚<a that="" passes="" variables="" to="" php

时间:2016-10-13 18:52:54

标签: javascript jquery html

="" I'm currently using javascript but switching to jQuery is OK.

Within the HTML is a <div id="domain">nares</div>. I need to pick out the "nares" and put it in an anchor tag to look like this: <a href=myCode.php?domain=nares" rel="modal:open">Listing</a>.

So far the code looks like this:

<script type="text/javascript">
 var dom = document.getElementById('domain').innerHTML; 
 openPage = function() {
 location.href = "myCode.php?domain="+dom;
}
</script>

<a href ="javascript:openPage()" rel="modal:open">Link  </a>

To me this looks like it should work but instead when the page opens I get;

TypeError: null is not an object (evaluating 'document.getElementById('domain').innerHTML')

This is probably because the div has not yet been populated. However after population the error changes to:

XMLHttpRequest cannot load javascript:openPage(). Cross origin requests are only supported for HTTP.

Which truthfully I don't understand. In any case using jQuery or just plain javascript how do I build this tag? I really need the modal part to work, its part of the UI for this project.

1 个答案:

答案 0 :(得分:1)

您应该将dom元素移动到函数中,如下所示:

<a id="test" onclick="openPage()" rel="modal:open">Link  </a>

<script type="text/javascript">
   function openPage() {
      var dom = document.getElementById('domain').innerHTML; 
      document.getElementById("test").href = "myCode.php?domain="+dom;
   }
</script>

此外,您应该获取a代码,然后更改href ...