使用第二个Onclick事件调用JS



我使用的是在iframe内打开URL的Onclick。它作为HREF链接工作得很好,但我决定将我的第二个URL更改为JS函数,而不是在每个链接中键入它。

这是我添加的JS:

function Link() {
  document.getElementById("TestMe").src = "http://www.cnn.com";
}

这是我原来的单元格链接,我试图调整与顶部脚本一起使用。我实际上需要将字符串.src='http://www.cnn.com'"更改为Link()函数。

<td bgcolor="#a7d331" height="10" align="center" style="cursor:pointer" onclick="window.open('http://www.yahoo.com'); document.getElementById('TestMe').src='http://www.cnn.com'"><font face="Arial" color="FFFFFF" size="4" alt="English">Some Link</font></td>
这是我的iframe:
<iframe id="TestMe" src="" frameborder="0" scrolling="no" width="400px" height="400px"></iframe>

用函数调用替换赋值

onclick="window.open('http://www.yahoo.com'); Link();"
<script>
    function link()
        {
            window.open('http://www.yahoo.com'); 
            document.getElementById('My_Test').src='http://www.cnn.com'
        }
</script>
<td onclick="link()"><font>Some Link</font></td>

您可以使用缩写this,即DOMElement

 onclick="window.open('http://www.yahoo.com'); this.href='http://www.cnn.com';"

请记住,一旦链接被点击两次,这将不会重新武装链接到yahoo.com。

td标签需要在表内才能成为有效的HTML;您要么需要将单元格包装在<table>元素中,要么将其更改为其他元素(例如span)。

<script type="text/javascript">
function Link() {
   document.getElementById("TestMe").src = "http://www.cnn.com";
}
</script>
<table>
<td bgcolor="#a7d331" height="10" align="center" style="cursor:pointer" onclick="window.open('http://www.yahoo.com'); Link();"><font face="Arial" color="FFFFFF" size="4" alt="English">Some Link</font></td>
</table>
<iframe id="TestMe" src="" frameborder="0" scrolling="no" width="400px" height="400px"></iframe>

最新更新