【问题标题】:Click function interfering with hrefs点击功能干扰hrefs
【发布时间】:2013-07-01 20:43:17
【问题描述】:

以下代码干扰了我所有的 html href 语句

$(
function(){
    // Get a reference to the content div (into which we will load content).
    var jContent = $( "#simplecart_items" );

    // Hook up link click events to load content.
    $( "a" ).click(function( e ){
            var jLink = $( this );

            // Override the click to load the contents at URL.
            jContent.load( jLink.attr( "href" ) );

            // Prevent default click.
            e.preventDefault();
        }
    );

}

);

干扰:

<ul class="one-row social">
<li class="updown-container"><a href="http://instagram.com/Blah" target="_blank"     class="updown icon-instagram"></a></li>
</ul>

有人可以帮我解释为什么以及可能如何解决这个问题吗?我已经尝试了一个小时..

【问题讨论】:

  • “干扰”是什么意思?你期待什么?
  • 你能贴出你想附加监听器的html吗?

标签: jquery html click href


【解决方案1】:

我猜是:

// Prevent default click.
e.preventDefault();

【讨论】:

    【解决方案2】:

    您是在告诉它影响每个 'a' 元素。

    我猜你只想在其他元素中影响“a”元素,比如“#cart”。

    如果是,请更改:

    $( "a" ).click(function( e ){
    

    收件人:

    $( "#cart a" ).click(function( e ){
    

    【讨论】:

    • 啊,它仍然无法正常工作..通过您的更改,我现在可以单击我的 href 链接,但是上面的代码不起作用..它基本上是假设在 DIV 中加载内容而不是刷新页面..我想我会继续尝试不同的东西..感谢您的意见!
    【解决方案3】:

    您尝试从中加载内容的 url 不允许 AJAX 访问。

    这里是代码,写得更好,更有效:

    var jContent = $("#simplecart_items");
    
    $(".social").find('a').on('click', function (e) {
        e.preventDefault();
    
        jContent.load($(this).attr("href"));
    
    });
    

    加载错误:XMLHttpRequest 无法加载 http://instagram.com/Blah。 Access-Control-Allow-Origin 不允许 Origin null。

    jsFiddle:http://jsfiddle.net/k82hR/

    【讨论】:

      猜你喜欢
      • 2016-06-07
      • 2021-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-14
      • 2017-05-12
      • 1970-01-01
      相关资源
      最近更新 更多