Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 引用已经在HTML链接标记中的ID(作为选择器)_Javascript_Jquery - Fatal编程技术网

Javascript 引用已经在HTML链接标记中的ID(作为选择器)

Javascript 引用已经在HTML链接标记中的ID(作为选择器),javascript,jquery,Javascript,Jquery,我有一个由核心模块生成的链接(意味着我不能修改代码),如下所示: 我的问题是id(当前的\u id)已经在标记中。 如何引用标记中的选择器。 我试过: $(current_id).replaceWith(result); //nothing happens $('#' + current_id).replaceWith(result); $('a#' + current_id).replaceWith(result); 但我得到了最后两个TypeError:Node.appendChild的参

我有一个由核心模块生成的链接(意味着我不能修改代码),如下所示:

我的问题是id(当前的\u id)已经在
标记中。 如何引用标记中的选择器。 我试过:

$(current_id).replaceWith(result); //nothing happens
$('#' + current_id).replaceWith(result);
$('a#' + current_id).replaceWith(result);
但我得到了最后两个TypeError:Node.appendChild的参数1没有实现接口节点

(我知道我可以做除replaceWith之外的其他事情,例如更改链接中的文本和href,但这里的问题是首先找到选择器)。

您可以使用
$(this)。replaceWith()

$(文档).on('click','my link class',function()){
var html='';
$(this).replacetwith(html);
返回false;
});
。它已关闭{
颜色:绿色;
}

我认为这里发生了两件事

  • 您正在尝试使用ID替换元素,而只保留对要替换的DOM元素的引用要比查找两次更容易

  • 您正在将事件绑定到锚定标记,然后尝试替换该锚定标记。一旦你更换它,事件就会消失。避免此问题的方法是将事件绑定到不会改变的内容。它可以是您要替换的元素正上方的元素,也可以是更高的元素,如
    body
    元素

  • 这里有一个可能解决这两个问题的解决方案。我编写了一个名为
    simulatedAjax
    的函数,让您了解后端代码的作用。它遵循与jQuery$.get相同的思想,使用
    configurationObject,callback(result)
    签名

    function simulatedAjax(config, done){
      var onOffText = (config.url === "on" ? "off" : "on");
    
      done('<a href="'+onOffText+'" id="custom-thing" class="custom-link">Switch '+ onOffText +'</a>');
    
    }
    
    您可以看到这个代码示例在这个JSFIDLE中工作


    希望这有帮助

    $(a.my-link-class)
    在语法上不正确。这仅仅是因为它是示例代码吗?你可以用它们的href来获取它们,比如
    $('[href=“/switch off”]')
    Typo-fixed$(“.my link class”)@Ted页面上有很多链接。我必须使用唯一的ID。
    $(".my-link-class").click(function() {
      var current_id = $(this).attr('id');
      var link = $(this).attr('href');
    
      $.ajax({url: link, success: function (result) {
      //All works fine up to here. The changes are made in server side and returns the new link as the result. 
    //Following is my problem:
    
        if(result){ 
          $(current_id).replaceWith(result); //the selector is wrong, I know.
        }
      }
    }
    
    $(current_id).replaceWith(result); //nothing happens
    $('#' + current_id).replaceWith(result);
    $('a#' + current_id).replaceWith(result);
    
    function simulatedAjax(config, done){
      var onOffText = (config.url === "on" ? "off" : "on");
    
      done('<a href="'+onOffText+'" id="custom-thing" class="custom-link">Switch '+ onOffText +'</a>');
    
    }
    
    $(function(){
    
      // Bind the click to the body element, but with a delegate to your link class .custom-link
      $('body').on('click', '.custom-link', function(e){
    
         // Store a reference to the A tag, name is irrelevant but self is easy to understand
         var self = this;
    
         // Keep the page from actually navigating to the href
          e.preventDefault();
    
          //Replace with real jQuery $.get or $.ajax with configuration
          simulatedAjax({
              url: $(this).attr('href')
          }, function(resultHTML){
    
              // Since we stored a reference to the original in the self variable, we can just replace it here. Note that if we tried to use `this` here, it wouldn't refer to the right `this`
              $(self).replaceWith(resultHTML);
          });
      });
    });