【问题标题】:using a <a> to run a function()使用 <a> 运行 function()
【发布时间】:2026-01-10 22:35:02
【问题描述】:

我想知道这个链接会调用我想要的函数吗?

<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,但它不会用新的HTML替换,它是一个

更新:这是工作代码:

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,因为没有它我可以实现我所需要的。感谢所有试图帮助我的人!!!。

【问题讨论】:

    标签: jquery function hyperlink tags


    【解决方案1】:

    没有。

    去掉内联的onclick 属性,然后这样做:

    <script type="text/javascript">
    $(function(){
        $('#caShip').click(function() {
            $(this).replaceWith('<div id="' + this.hash.slice(1) + '">some HTML</div>');
               // uncomment the next line if you want the hash to actually appear.
            // window.location.hash = this.hash;
    
               // prevent the page from reloading
            return false;
        });
    });
    </script>
    

    如果你确实想要它内联,你可以这样做:

    <a href="http://catalog.bitsltd.us/power_strips#replaced" onclick="caShip.call(this);" id="caShip">Some Text</a>
    
    
    <script type="text/javascript">
    function caShip(){
        $(this).replaceWith('<div id="' + this.hash.slice(1) + '">some HTML</div>');
    }
    </script>
    

    编辑: 已修复它以使用 Anchor 元素中的 hash 创建具有该 ID 的新元素,因为用于替换的文本似乎暗示了这一点。

    【讨论】:

    • 在这种情况下,这绝对是要走的路。但是,您并不总是(想要)知道调用函数的元素的 id。或者它可能是设计使然。两种方式都可以正常工作,在这个例子中我认为 jquery click() 很棒!
    • 似乎不起作用。重新加载页面但没有变化。旧的 HTML 仍在页面中。
    • @MJ3111:如果它与您当前所在的页面相同(听起来像),那么您可以将&lt;a&gt;href 更改为"#replaced",而不是完整的域和路径名。如果这不是一个选项,那么我们需要阻止&lt;a&gt; 的默认行为,并手动将哈希更新为#replaced。告诉我。
    • ...如果您无法更改href,并且我们确实需要阻止&lt;a&gt; 的默认行为,您需要让我知道您是否还在是否使用内联 onclick。解决方案会有所不同。
    • 如果可能的话,我不想使用链接.... href 必须是完整的 URL,因为该页面的基本 URL 与页面的实际 URL 不同。这是故意这样做的。我不想使用内联 onclick。我希望它最初只是 div 中的一些文本,然后替换为 div 中的一些不同文本。当我尝试它对我也不起作用时。
    【解决方案2】:
    <a href="http://catalog.bitsltd.us/power_strips#replaced" onclick="caShipFunc();" id="caShip">Some Text</a>
    
    
    <script type="text/javascript">
      function caShipFunc(){
          $('#caShip').replaceWith('Some HTML (the new HTML has an id of replaced)');
      }
    </script>
    

    【讨论】:

      【解决方案3】:

      由于你使用的是 jQuery,你应该这样做:

      <a href="http://catalog.bitsltd.us/power_strips#replaced" id="caShip">Some Text</a>
      

      jQuery

      <script type="text/javascript">
      $(function(){       
          $('#caShip').click(function() {
              if($(this).attr('href').indexOf("#") != -1) {
                  $('#caShip').replaceWith('<div>Test</div>');
              }
          });
      });
      </script>
      

      编辑: 更新了 jQuery 以解决您的评论。这适用于我非常基本的测试。

      【讨论】:

      • 运气不好。页面重新加载但没有变化。
      • 2 个问题... 一:双引号是故意的吗? 。指数(”#”)。 2 第一个右括号需要一个“;”吗?我想我需要弄清楚如何在不重新加载页面的情况下做到这一点。有什么建议么?我确实尝试了更新但没有运气
      • 感谢您的帮助。如果您查看我的原始问题,您将看到我的工作代码。
      【解决方案4】:

      试试这个...

      HTML

      <a href="#replaced" id="caShip">Some Text</a>
      

      JS

      $('#caShip').click(function(){
          $(this).text('Some HTML (the new HTML has an id of replaced)');
      });
      

      http://jsfiddle.net/8bUKs/1/

      【讨论】:

      • 我在这方面有点晚了。哈。
      • 我会这样做,只是我不能只使用 '#replaced' 作为 href,因为该页面的基本 URL 与当前页面不同。我已经尝试了每个人的建议,但没有运气。它会重新加载页面,但不会将 替换为 js 中的 HTML。
      • 那是因为当它重新加载页面时,你对 JS 所做的任何事情都会被重置,因为它是客户端。
      • 那么我如何在重新加载时调用该函数?我可以吗?
      • 如果您的链接将您带到另一个页面,则该页面会检测您来自哪里并相应地显示正确的文本。 Javascript 仅用于您当前所在的页面(当涉及更改屏幕上的元素时)。离开该页面后,您也将保留所有更改。不知道自己到底想做什么会让事情变得很艰难。
      【解决方案5】:

      你误用了 jQuery 而实际上并没有声明一个函数。

      编写$(function someName() { ... }) 创建一个命名函数表达式并将其传递给$ 函数。 它没有声明可重用的函数;然后你不能写someName()

      此外,您在 onclick 处理程序中的调用是完全错误的。
      为了调用一个函数,你应该调用它(onclick="caShip();");你不能把它和getElementById结合起来。

      执行此操作的正确方法是使用 jQuery 添加 click 处理程序:

      $(function() { 
          $('#caShip').click(function() {
              $(this).replaceWith('Some HTML (the new HTML has an id of replaced)');
          });
      });
      

      【讨论】:

      • 我已经尝试过了,我猜你建议的 HTML 部分会类似于 Michael I 或 Dutchie?如果这是正确的,那么它没有帮助。 HTML 没有被替换。似乎页面正在重新加载(因为我假设的 href )。也许这样做我需要在页面重新加载时调用该函数?那可能吗?此外,我不能只使用 '#reloaded' 作为 href,因为该页面具有不同的基本 URL。
      • 感谢您的帮助。如果您查看我的原始问题,我已使用工作代码对其进行了更新。
      最近更新 更多