使用 运行一个函数()
我想知道这个链接会调用我想要的函数吗?
<a href="http://catalog.bitsltd.us/power_strips#replaced" onclick="document.getElementById('caShip').function caShip()" id="caShip">Some Text</a>
这是我想调用的函数...谁能告诉我为什么它也不起作用?
<script type="text/javascript">
$(function caShip(){
$('#caShip').replaceWith('Some HTML (the new HTML has an id of replaced)');
});
</script>
单击链接时,它会转到 href,这是同一页面和一个 ID,但它不会替换 << a>使用新的 HTML,它是 < div >?
更新:这是工作代码:
js
<script type="text/javascript">
$(document).ready(function(){
//Hide Canadian Information
$('.locationDiv').hide();
//bind the links click events
$('.locLink').click(function(){
$('#1').hide();
$('#desc_' + $(this).attr('title')).show();
});
});
</script>
HTML
<a href="javascript:void(0);" title="can" class="locLink" id="1">Canadian Customers Click Here Before Ordering!</a>
<div id="desc_can" class="locationDiv">
<table class="contentpaneopen">
<tr>
<td class="contentheading" width="100%">Attention Canadian Customers!
</td>
</tr>
</table>
<table class="contentpaneopen">
<tr>
<td valign="top" >
<span class="body">Please note that there are fees associated with shipping to Canada from the US that are <b><u><i><font color="red">NOT</font></i></u></b> included in the cost of the shipping or the cost of the unit. These cost are to be paid for by the purchaser. Here are some tips for shipping to Canada:
<br />
<br />
-USPS methods are cheap but very unreliable. <b>Border fees</b> are not charged using USPS, only UPS or Fed Ex (which are the most reliable).
<br />
-<b>Customs fees</b> can sometime run <b>up to 50%</b> of the purchase price (UPS/FedEx).
<br />
-Smart Strips are available from a Canadian dealer. Visit our <a href="index.php?Itemid=146" title="Store Locator" target="_blank">Store Locator</a> to find a local seller.
<br />
-Customers with a UPS or FedEx account may ship on their account and assume all fees with no delays.
<br />
-Canadian customers selecting UPS or FedEx will have to pick the package up at their local station and pay the fees. So you order it online, but still have to drive and pay to pick it up unless you used your own UPS/Fed Ex account.</span>
</td>
</tr>
</table>
</div>
我没有使用CSS,因为没有它我也可以实现我需要的东西。感谢所有试图帮助我的人!
I was wondering will this link call the function I want?
<a href="http://catalog.bitsltd.us/power_strips#replaced" onclick="document.getElementById('caShip').function caShip()" id="caShip">Some Text</a>
and this is the function I want to call... Can anyone tell me why it is not working also?
<script type="text/javascript">
$(function caShip(){
$('#caShip').replaceWith('Some HTML (the new HTML has an id of replaced)');
});
</script>
When the link is clicked it does go to the href, which is the same page and an ID, but it doesn't replace the < a > with the new HTML, which is a < div >?
UPDATE: THIS IS THE WORKING CODE:
js
<script type="text/javascript">
$(document).ready(function(){
//Hide Canadian Information
$('.locationDiv').hide();
//bind the links click events
$('.locLink').click(function(){
$('#1').hide();
$('#desc_' + $(this).attr('title')).show();
});
});
</script>
HTML
<a href="javascript:void(0);" title="can" class="locLink" id="1">Canadian Customers Click Here Before Ordering!</a>
<div id="desc_can" class="locationDiv">
<table class="contentpaneopen">
<tr>
<td class="contentheading" width="100%">Attention Canadian Customers!
</td>
</tr>
</table>
<table class="contentpaneopen">
<tr>
<td valign="top" >
<span class="body">Please note that there are fees associated with shipping to Canada from the US that are <b><u><i><font color="red">NOT</font></i></u></b> included in the cost of the shipping or the cost of the unit. These cost are to be paid for by the purchaser. Here are some tips for shipping to Canada:
<br />
<br />
-USPS methods are cheap but very unreliable. <b>Border fees</b> are not charged using USPS, only UPS or Fed Ex (which are the most reliable).
<br />
-<b>Customs fees</b> can sometime run <b>up to 50%</b> of the purchase price (UPS/FedEx).
<br />
-Smart Strips are available from a Canadian dealer. Visit our <a href="index.php?Itemid=146" title="Store Locator" target="_blank">Store Locator</a> to find a local seller.
<br />
-Customers with a UPS or FedEx account may ship on their account and assume all fees with no delays.
<br />
-Canadian customers selecting UPS or FedEx will have to pick the package up at their local station and pay the fees. So you order it online, but still have to drive and pay to pick it up unless you used your own UPS/Fed Ex account.</span>
</td>
</tr>
</table>
</div>
I didn't use the CSS because I can achieve what I need without it. Thanks to everyone that was trying to help me with this!!!.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不。
摆脱内联
onclick
属性,然后执行以下操作:如果您确实希望内联它,则可以执行以下操作:
编辑: 将其修复为使用 Anchor 元素中的
hash
来创建具有该 ID 的新元素,因为这就是用于替换的文本似乎所暗示的内容。No.
Get rid of the inline
onclick
attribute, and just do this:If you did want it inline, you could just do this:
EDIT: Fixed it to use the
hash
from the Anchor element to create a new element with that ID, since that's what the text used for the replacement seems to imply.由于您使用的是 jQuery,因此您应该执行以下操作:
jQuery
编辑: 更新了 jQuery 以解决您的评论。这在我非常基本的测试中有效。
Since you're using jQuery, you should do something like this:
jQuery
EDIT: Updated the jQuery to address your comment. This works in my very basic testing.
试试这个...
HTML
JS
http://jsfiddle .net/8bUKs/1/
Try this...
HTML
JS
http://jsfiddle.net/8bUKs/1/
您滥用了 jQuery 并且实际上没有声明函数。
编写
$(function someName() { ... })
创建一个命名函数表达式并将其传递给$
函数。它没有声明可重用的函数;然后你就不能写
someName()
。此外,您在
onclick
处理程序中的调用是完全错误的。为了调用函数,您应该直接调用它 (
onclick="caShip();"
);您不能将其与getElementById
结合使用。正确的方法是使用 jQuery 添加一个
click
处理程序:You're misusing jQuery and not actually declaring a function.
Writing
$(function someName() { ... })
creates a named function expression and passes it to the$
function.It doesn't declare a reusable function; you cannot then write
someName()
.In addition, your call in the
onclick
handler is completely wrong.In order to call a function, you should just call it (
onclick="caShip();"
); you can't combine it withgetElementById
.The correct way to do this is to add a
click
handler using jQuery: