标签: replacewith

替换XML问题

在检查了jQuery源代码之后,我发现我遇到的问题是因为XML文档不存在的replaceWith调用html.是replaceWith不是应该在XML文档上工作?

我发现这个公认的简单解决方法,以防将来有人需要它,这将完成我正在尝试做的事情:

xml.find('b').each(function() {
    $(this).replaceWith($('<c>yo</c>')) // this way you can custom taylor the XML based on each node's attributes and such
});
Run Code Online (Sandbox Code Playgroud)

但我仍然想知道为什么简单的方法不起作用.


我对jQuery了解不多,但不应该这样吗?

xml = $.parseXML('<a><b>hey</b></a>')
$(xml).find('b').replaceWith('<c>yo</c>')
Run Code Online (Sandbox Code Playgroud)

而不是xml代表<a><c>yo</c></a>它失败和代表<a></a>.我做错什么了吗?我正在使用jQuery 1.6.2.

编辑:

作为旁注,如果我尝试使用函数版本replaceWith,如下:

$(xml).find('b').replaceWith(function() {
    return '<c>yo</c>' // doesn't matter what I return here
})
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

TypeError: Cannot call method 'replace' of undefined
Run Code Online (Sandbox Code Playgroud)

编辑2:

replaceAll 然而,但我需要使用函数版本,所以我不能满足于此:

$('<c>yo</c>').replaceAll($(xml).find('b')) // works
Run Code Online (Sandbox Code Playgroud)

编辑3:

这也有效:

xml.find('b').replaceWith($('<c>yo</c>')) // but not with the …
Run Code Online (Sandbox Code Playgroud)

javascript xml jquery replacewith

6
推荐指数
1
解决办法
615
查看次数

在自然JavaScript中编写jQuery的replaceWith()的最佳方法

虽然功能很快就会打到页面,所以我需要用纯JavaScript编写它并将其包含在头部.不确定如何去做,因为据我所知,通过使用.replace(),新元素将被移动到页面上的不同位置.jQuery的replaceWith()行为是理想的.

$("#imagefiles").replaceWith("<input type='file' name='imagefiles' id='imagefiles' />");
Run Code Online (Sandbox Code Playgroud)

javascript jquery load replacewith

6
推荐指数
1
解决办法
9569
查看次数

"jQuery方式"用HTML和文本混合替换一个文本节点?

在我的Web浏览器用户脚本项目中,我需要替换一个文本节点,而不会影响与文本相同的父节点下的其他HTML元素.我需要用多个节点替换它:

<div id="target"> foo / bar; baz<img src="/image.png"></div>
Run Code Online (Sandbox Code Playgroud)

需要成为:

<div id="target"> <a href="#">foo</a> / <a href="#">bar</a>; <a href="#">baz</a><img src="/image.png"></div>
Run Code Online (Sandbox Code Playgroud)

我知道jQuery没有对文本节点的大量支持.我知道我可以使用直接DOM调用而不是jQuery.而且我知道我可以做一些像$('#target').html(+不想改变的新东西).另外请注意,我想保留最初的空间,这本身似乎很棘手.

我想问一下这里的专家是,有一种最惯用的jQuery方法吗?

jquery dom-manipulation replacewith

6
推荐指数
1
解决办法
5872
查看次数

替换元素并保留属性

几乎以下工作在替换的所有实例span[data-type="yesno"]与LIS,但我想也保留属性,类等是否有结转以同样的方式作为HTML属性的方式吗?

$('span[data-type="yesno"]').replaceWith(function(){
    return $("<li>", {html: $(this).html()});
})
Run Code Online (Sandbox Code Playgroud)

jquery dom replacewith

6
推荐指数
1
解决办法
1611
查看次数

jQuery .replaceWith()替代

我的网站使用jQuery 1.4.2.问题是.replaceWith()在jQuery 1.4.2中的IE6和IE7中不起作用.在jQuery 1.4.2中是否有IE6和IE7支持的替代方法?

小提琴在这里:http://jsfiddle.net/8CEwf/1/

我知道,似乎jQuery没有附加到它,但是如果你看一下HTML,jQuery就在那里,因为jsFiddle没有提供1.4.2版本

HTML:

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<img src="/v/vspfiles/templates/cyberfront/images/buttons/btn_addtocart_small.gif">
<input type="image" src="/v/vspfiles/templates/cyberfront/images/buttons/btn_go_gray.gif">
<img src="/v/vspfiles/templates/cyberfront/images/Bullet_MoreInfo.gif">
Run Code Online (Sandbox Code Playgroud)

脚本:

$(document).ready(function(){
$('img[src="/v/vspfiles/templates/cyberfront/images/buttons/btn_addtocart_small.gif"]').replaceWith('<br /><span id="blackbutton" class="mediumbutton" style="display:block;">Add to Cart</span>');
$('input[src="/v/vspfiles/templates/cyberfront/images/buttons/btn_go_gray.gif"]').replaceWith('<input type="submit" class="graybutton smallbutton" name="Go" alt="Go" value="Go" title="Go">');
$('img[src="/v/vspfiles/templates/cyberfront/images/Bullet_MoreInfo.gif"]').replaceWith('<span class="learnmore">Learn More</span>');
});
Run Code Online (Sandbox Code Playgroud)

jquery replace internet-explorer-7 internet-explorer-6 replacewith

5
推荐指数
1
解决办法
4514
查看次数

jQuery 移除 Font Awesome 图标

我在一个实验中使用了 Font Awesome 和 jQuery,在一个动画之后,jQuery 用一个检查替换了列表中的加载图标,并且文本也发生了变化。但是,当我删除 jQueryreplaceWith方法时,图标保持不变:

HTML:

<ul class="fa-ul">
        <li><i class="fa-li fa fa-spinner fa-spin" id = "ToDo1"></i>Downloading</li>
        <li><i class="fa-li fa fa-square" id = "ToDo2"></i>text</li>
        <li><i class="fa-li fa fa-square" id = "ToDo3"></i>text</li>
        <li><i class="fa-li fa fa-square" id = "ToDo4"></i>text</li>
</ul>
Run Code Online (Sandbox Code Playgroud)

jQuery:

setTimeout(function() {
    $("#ToDo1").removeClass("fa-spin fa-spinner");
    $("#ToDo1").addClass("fa-check-square");
    $("li:first").replaceWith("Download Complete");
    $("#ToDo2").addClass("fa-spin fa-spinner");
}, 2000);
Run Code Online (Sandbox Code Playgroud)

结果:

前:

在此处输入图片说明

jQuery 之后 replaceWith

在此处输入图片说明

问题是什么?

html javascript jquery replacewith

5
推荐指数
1
解决办法
3821
查看次数

replaceWith第二次不起作用

我需要单击列表项以使用xml文件中的节点中的文本替换div中的文本.

我单击列表项并将结果存储在变量中.然后我加载xml,找到我需要的节点的标题,并创建结果的变量.然后我运行if语句,如果两个变量相等,则将xml放入div中.

我第一次点击列表项,这是有效的.列表中的正确项与xml中的正确节点匹配,正确的文本放在div中.

但是,当我单击其他列表项时,文本不会被替换.警报显示第二次单击获得了正确的信息,但最终,div内容未被替换.

在这里演示:http://mwebphoto.com/mwebphoto/html/3rdJqueryPage.html

代码在这里:

<body>

<div class="slideshowContainer">
<div id="slideshow"></div>
</div>

<ul id="gallery_id">
  <li id="newYork">New York</li>
  <li id="disconnection">Disconnexion</li>
  <li id="jackAtSea">Jack at Sea</li>
</ul>

<script>

  $(document).ready(function(){ 

  $("#gallery_id li").on('click', function(e) {

     var htmlTitle = (this.id);

$.ajax({
  type: "GET",
    url: "/mwebphoto/xml/albums.xml",
    dataType: "xml",
    success: function(xml) {
        $(xml).find('photoAlbum').each(function(){
            var xmlAlbum= $(this);
            var xmlTitle = $(this).find('title').text();
            var xmlEmbedCode = $(this).find('embedCode').text();
                alert ('this is html titled  ' + htmlTitle);
                alert ('this is xml titled  ' + xmlTitle);
            if(xmlTitle=htmlTitle)

             alert ('this is matched …
Run Code Online (Sandbox Code Playgroud)

jquery replacewith

4
推荐指数
1
解决办法
3313
查看次数

将字符串替换为下一行的大写字母(Pascal Casing)

我想从字符串中删除所有下划线,并在下划线后面加上字符的大写字母.所以例如:_my_string_变成:MyString相似:my_string成为MyString

有更简单的方法吗?我目前有以下(假设没有输入有两个连续的下划线):

        StringBuilder sb = new StringBuilder();
        int i;
        for (i = 0; i < input.Length - 1; i++)
        {
            if (input[i] == '_')
                sb.Append(char.ToUpper(input[++i]));
            else if (i == 0)
                sb.Append(char.ToUpper(input[i]));
            else
                sb.Append(input[i]);
        }
        if (i < input.Length && input[i] != '_')
            sb.Append(input[i]);

        return sb.ToString();
Run Code Online (Sandbox Code Playgroud)

现在我知道这并不是完全相关的,但我想在答案中提供的实现上运行一些数字,这里是使用1000000 iterations字符串的每个实现的毫秒结果"_my_string_121_a_":

Achilles: 313
Raj: 870
Damian: 7916
Dmitry: 5380
Equalsk: 574
Run Code Online (Sandbox Code Playgroud)

方法:

        Stopwatch stp = new Stopwatch();
        stp.Start();
        for (int i = 0; …
Run Code Online (Sandbox Code Playgroud)

c# string replace replacewith

4
推荐指数
1
解决办法
613
查看次数

jQuery AJAX使用fadeIn + replaceWith

SO上有几个q&a's,但我发现没有解决我的问题.而且我很难过.如果您知道可用的答案,请将我重定向到链接.

我的页面空DIV #posts_insert了,通过Ajax插入新帖子.

    $.ajax({
        url: 'chat/posts_submit/' + <?php echo $page_id; ?>,
        type: 'POST',
        data: $('#posts_form').serialize(),
        dataType: 'html',
        success: function(html) {
            $('#posts_insert').replaceWith(html);
        }
    });
Run Code Online (Sandbox Code Playgroud)

我希望这个新帖子能够淡入,取而代之#posts_insert.我已经尝试了几次success使用hide()之前的迭代,fadeIn但我无法使这项工作.

任何想法如何解决这个问题?提前致谢.

jquery hide fadein replacewith

3
推荐指数
1
解决办法
3599
查看次数

单击时将td更改为文本框,并在用户完成后将文本框更改回td

我正在寻找一种方法,用户可以通过点击它来改变表中的td.我当前的设置是当用户点击某个td时,它应该用一个文本框替换单击的td,该文本框包含点击的td中的文本.然后,当用户点击它之外,或按回车键.它应该将文本框更改回td.

这是我的表:

<table class="table" id="tableCompleted">
    <thead>
        <tr>
            <th>Name</th>
            <th>A</th>
            <th>B</th>
            <th>C</th>
            <th>D</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>John</td>
            <td>Yes</td>
            <td>Yes</td>
            <td>No</td>
            <td>No</td>
        </tr>
        <tr>
            <td>Jesse</td>
            <td>Yes</td>
            <td>Yes</td>
            <td>Yes</td>
            <td>Yes</td>
        </tr>
        <tr>
            <td>David</td>
            <td>No</td>
            <td>Yes</td>
            <td>No</td>
            <td>No</td>
        </tr>
    </tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

这是我写的脚本,我遇到的问题是,当用户点击某个td时,它会替换掉所有的td,并且我没有想出如何在失去焦点时将其更改回来.我尝试使用按钮,但这更糟糕.此外,来自td的文本不会放在文本框中.

我用它的脚本:

$('td').click(function () {
    $('td').replaceWith(function () {
        return '<input type="text" id="modify" "value="' + $('#Text').val() + '" />';
    });
})
Run Code Online (Sandbox Code Playgroud)

我目前有3个问题,我不知道如何只更改被点击的td,因为所有的td都没有唯一的id或类.当用户完成更改后,我不确定如何更改它.此外,它不会将td的内容放在文本框中.

javascript jquery html-table replacewith

2
推荐指数
1
解决办法
1万
查看次数