0

我不知道如何在<a href>.

例如:

var x =  document.getElementById('example').value // value is 6 now
<a href="javascript:function6();">+</a>

document.write应该看起来像这样:

document.write('<a href="javascript:function"' + x + '();">Something</a>'

问题是,我必须使用 inner.html。

我有这样的代码:

<script type="text/javascript">

   function count(){ 
       var x = 1;
       document.getElementById('count').value = ++x;  
       var z = x + 4;
   }

   var ex = ("<a class=\"button6\" href=\"javascript:anotherfunction\" + z + '()'>Anything else<\/a>");

   function new(){
       document.getElementById("add").innerHTML += ex
   }
</script>
<table id="add">
  <tr>
    <td>
      <a class="button6" href="javascript:new();count();">+</a>
    </td>
  </tr>
</table>

“+”-链接应该在 href 的函数名称中添加另一个带有计数器值的链接,如下所示:

<a class="button6" href="javascript:anotherfunction6();">+</a>

代码中的第一个“+”链接应该在 href 处添加第二个链接,其中包含函数名称中的计数器值,因此单击“+”链接两次后添加的代码应该是这样的:

<a class="button6" href="javascript:anotherfunction6();">+</a>
<a class="button6" href="javascript:anotherfunction7();">+</a>
...

但它不起作用。

4

3 回答 3

0

To make your code work, you should fix:

1) Escape sequence.

`var ex = '<a class="button6" href="javascript:anotherfunction' + z + "()\">Anything else<\/a>";`

2) Function name. new is a keyword and could not be used as a function name.

3) Variable scopes. Variable z should be visible to both functions and variable ex should be set every time you are creating new reference.

After all fixes the code looks like:

var z = 0;
function count() { 
    var x = 1;
    document.getElementById('count').value = ++x;  
    z = x + 4;
}

function createButton() {
    var ex = '<a class="button6" href="javascript:anotherfunction' + z + "()\">Anything else<\/a>";
    document.getElementById("add").innerHTML += ex
}`

Also, you should fix your HTML code:

<a class="button6" href="javascript:createButton();count();">+</a>

P.S. Using javascript in <a> is a bad code style. Generating a bunch of functions like anotherfunctionN is also is not a good idea.

于 2013-01-14T11:55:13.910 回答
0

\" + z + '()应该是" + z + "'()。但是,您的代码还有其他问题,因此“不起作用”可能有几个原因。

于 2013-01-14T11:33:23.257 回答
0

查看您的代码..我看到您没有正确地转义您的代码..

尝试这个

var ex = ("<a class=\"button6\" href=\"javascript:anotherfunction\"" + z + "'()'>Anything else<\/a>");
于 2013-01-14T11:35:20.347 回答