为什么jQuery选择器不起作用?

Isi*_*lor 0 javascript jquery

为什么这不起作用?

        $(document).ready(function(){

            var content = '<a href="http://example.com/index.php><b>Some text!</b></a> - <a href="http://example.com/index.php" class="ot-origin-anchor">News</a>';

            $("#test").replaceWith(function(){

                return content;

            });
            //Here is the problem. I don't know why but I can't define adres.
            var adres = $("#test .ot-origin-anchor").attr("href");

            //find example.com - ugly :P
            var adresRegExp = adres.match(/(\w+:\/\/+(www.|))([^/]+)/);

            alert(RegExp.$3);

        });

    </script>

    <div id="test">bnb</div>
Run Code Online (Sandbox Code Playgroud)

Mat*_*all 6

.replaceWith()通话结束后,页面上没有带ID的元素test.看起来你打算使用.html().append()代替.replaceWith().

var content = '<a href="http://example.com/index.php><b>Some text!</b></a> - <a href="http://example.com/index.php" class="ot-origin-anchor">News</a>';

$("#test").html(content);
// or
$("#test").append(content);

var adres = $("#test .ot-origin-anchor").attr("href");
Run Code Online (Sandbox Code Playgroud)