简体   繁体   English

使用第二个Onclick事件调用JS

[英]Using an second Onclick event to call JS

I'm using an Onclick that opens a URL inside an iframe. 我使用的Onclick会在iframe中打开一个URL。 It works great as an HREF link, but I've decided to change my second URL into JS function instead of typing it in each of my links. 它非常适合作为HREF链接,但是我决定将第二个URL更改为JS函数,而不是在每个链接中都键入它。

Here is my added JS: 这是我添加的JS:

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

This is my original Cell link that I'm trying to adjust to be used with the top script. 这是我尝试调整为与顶级脚本一起使用的原始Cell链接。 I actually need to change the string .src='http://www.cnn.com'" into a Link() function. 实际上,我需要将字符串.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>

Here is my iframe: 这是我的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>

You can use short-hand this which is the DOMElement 您可以使用简短的this这是一个DOMElement

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

Keep in mind this will not re arm the link to yahoo.com once the link is clicked twice. 请记住,一旦单击两次链接,就不会撤消到yahoo.com的链接。

The td tag needs to be inside a table in order to be valid HTML; td标记必须在表中才能有效HTML。 You either need to wrap your cell in a <table> element, or change it to a different element (such as a span). 您需要将单元格包装在<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>

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

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