{"id":44,"date":"2025-04-07T19:26:01","date_gmt":"2025-04-07T11:26:01","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/?p=44"},"modified":"2025-04-07T19:26:01","modified_gmt":"2025-04-07T11:26:01","slug":"%e5%a6%82%e4%bd%95%e5%b0%86domxpath%e8%8e%b7%e5%8f%96%e8%bd%ac%e5%8f%98%e4%b8%ba%e7%b1%bb%e4%bc%bcjquery%e7%9a%84%e8%8e%b7%e5%8f%96%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/www.zhaozhao123.cn\/php\/post\/44.html","title":{"rendered":"\u5982\u4f55\u5c06DOMXPath\u83b7\u53d6\u8f6c\u53d8\u4e3a\u7c7b\u4f3cjQuery\u7684\u83b7\u53d6\u65b9\u5f0f"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u7b80\u5355\u5b9e\u73b0\u4e00<\/h2>\n\n\n\n<p>\u5728 PHP \u4e2d\u4f7f\u7528 <code>DOMXPath<\/code> \u8fdb\u884c DOM \u64cd\u4f5c\u65f6\uff0c\u5176\u529f\u80fd\u7c7b\u4f3c\u4e8e jQuery \u7684\u9009\u62e9\u5668\u64cd\u4f5c\uff0c\u4f46\u8bed\u6cd5\u548c\u7528\u6cd5\u4e0a\u5b58\u5728\u660e\u663e\u5dee\u5f02\u3002\u5982\u679c\u4f60\u5e0c\u671b\u5c06 <code>DOMXPath<\/code> \u7684\u83b7\u53d6\u65b9\u5f0f\u8f6c\u53d8\u4e3a\u7c7b\u4f3c jQuery \u7684\u98ce\u683c\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c01\u88c5\u4e00\u4e2a\u7c7b\u6216\u51fd\u6570\u6765\u5b9e\u73b0\u66f4\u7b80\u6d01\u7684\u94fe\u5f0f\u8c03\u7528\u548c\u7c7b\u4f3c jQuery \u7684 API\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5b9e\u73b0\u601d\u8def\uff1a<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u5c01\u88c5\u4e00\u4e2a\u7c7b<\/h3>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5c01\u88c5\u4e00\u4e2a\u7c7b\uff0c\u5c06 <code>DOMDocument<\/code> \u548c <code>DOMXPath<\/code> \u7684\u64cd\u4f5c\u9690\u85cf\u8d77\u6765\uff0c\u5e76\u63d0\u4f9b\u7c7b\u4f3c jQuery \u7684\u65b9\u6cd5\u540d\u548c\u94fe\u5f0f\u8c03\u7528\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Query {\n    private $dom;\n    private $xpath;\n    private $nodes = &#91;];\n\n    public function __construct($html = null) {\n        $this-&gt;dom = new DOMDocument();\n        libxml_use_internal_errors(true); \/\/ \u9632\u6b62 HTML \u89e3\u6790\u9519\u8bef\u629b\u51fa\u5f02\u5e38\n        if ($html) {\n            $this-&gt;dom-&gt;loadHTML($html);\n        }\n        $this-&gt;xpath = new DOMXPath($this-&gt;dom);\n        libxml_clear_errors();\n    }\n\n    \/\/ \u7c7b\u4f3c jQuery \u7684 find \u65b9\u6cd5\n    public function find($selector) {\n        $this-&gt;nodes = $this-&gt;xpath-&gt;query($selector);\n        return $this; \/\/ \u652f\u6301\u94fe\u5f0f\u8c03\u7528\n    }\n\n    \/\/ \u83b7\u53d6\u6240\u6709\u5339\u914d\u7684\u8282\u70b9\u5185\u5bb9\n    public function text() {\n        $texts = &#91;];\n        foreach ($this-&gt;nodes as $node) {\n            $texts&#91;] = $node-&gt;nodeValue;\n        }\n        return $texts;\n    }\n\n    \/\/ \u83b7\u53d6\u7b2c\u4e00\u4e2a\u5339\u914d\u8282\u70b9\u7684\u5185\u5bb9\n    public function firstText() {\n        if ($this-&gt;nodes-&gt;length &gt; 0) {\n            return $this-&gt;nodes-&gt;item(0)-&gt;nodeValue;\n        }\n        return null;\n    }\n\n    \/\/ \u8bbe\u7f6e\u6216\u83b7\u53d6 HTML\n    public function html($newHtml = null) {\n        if ($newHtml !== null) {\n            foreach ($this-&gt;nodes as $node) {\n                $fragment = $this-&gt;dom-&gt;createDocumentFragment();\n                $fragment-&gt;appendXML($newHtml);\n                $node-&gt;appendChild($fragment);\n            }\n            return $this;\n        }\n        $htmls = &#91;];\n        foreach ($this-&gt;nodes as $node) {\n            $htmls&#91;] = $this-&gt;dom-&gt;saveHTML($node);\n        }\n        return $htmls;\n    }\n\n    \/\/ \u83b7\u53d6\u6240\u6709\u5339\u914d\u7684\u8282\u70b9\u5c5e\u6027\n    public function attr($attribute) {\n        $attrs = &#91;];\n        foreach ($this-&gt;nodes as $node) {\n            if ($node-&gt;hasAttribute($attribute)) {\n                $attrs&#91;] = $node-&gt;getAttribute($attribute);\n            }\n        }\n        return $attrs;\n    }\n\n    \/\/ \u6dfb\u52a0\u65b0\u7684\u8282\u70b9\n    public function append($html) {\n        foreach ($this-&gt;nodes as $node) {\n            $fragment = $this-&gt;dom-&gt;createDocumentFragment();\n            $fragment-&gt;appendXML($html);\n            $node-&gt;appendChild($fragment);\n        }\n        return $this;\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u4f7f\u7528\u793a\u4f8b<\/h3>\n\n\n\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0b HTML \u5185\u5bb9\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$html = &lt;&lt;&lt;HTML\n&lt;html&gt;\n&lt;body&gt;\n    &lt;div class=\"container\"&gt;\n        &lt;p id=\"para1\"&gt;Hello, World!&lt;\/p&gt;\n        &lt;p id=\"para2\"&gt;This is a test.&lt;\/p&gt;\n    &lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\nHTML;\n\n$query = new Query($html);\n\n\/\/ \u67e5\u627e\u6240\u6709 p \u6807\u7b7e\u5e76\u83b7\u53d6\u5b83\u4eec\u7684\u6587\u672c\u5185\u5bb9\n$texts = $query-&gt;find('<a href=\"https:\/\/www.zhaozhao123.cn\/skin\/go?url=aHR0cDovL3A=\" rel=\"noopener noreferrer nofollow\">\/\/p<\/a>')-&gt;text();\nprint_r($texts);\n\n\/\/ \u67e5\u627e\u7b2c\u4e00\u4e2a p \u6807\u7b7e\u7684\u6587\u672c\u5185\u5bb9\n$firstText = $query-&gt;find('<a href=\"https:\/\/www.zhaozhao123.cn\/skin\/go?url=aHR0cDovL3A=\" rel=\"noopener noreferrer nofollow\">\/\/p<\/a>')-&gt;firstText();\necho $firstText;\n\n\/\/ \u83b7\u53d6\u6240\u6709 p \u6807\u7b7e\u7684 HTML\n$htmls = $query-&gt;find('<a href=\"https:\/\/www.zhaozhao123.cn\/skin\/go?url=aHR0cDovL3A=\" rel=\"noopener noreferrer nofollow\">\/\/p<\/a>')-&gt;html();\nprint_r($htmls);\n\n\/\/ \u4fee\u6539\u7b2c\u4e00\u4e2a p \u6807\u7b7e\u7684\u5185\u5bb9\n$query-&gt;find('<a href=\"https:\/\/www.zhaozhao123.cn\/skin\/go?url=aHR0cDovL3A=\" rel=\"noopener noreferrer nofollow\">\/\/p<\/a>&#91;@id=\"para1\"]')-&gt;html('&lt;strong&gt;New Content&lt;\/strong&gt;');\n\n\/\/ \u8f93\u51fa\u4fee\u6539\u540e\u7684\u5b8c\u6574 HTML\necho $query-&gt;html();<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u7ed3\u679c\u89e3\u91ca<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>find<\/code> \u65b9\u6cd5<\/strong>\uff1a\u652f\u6301 XPath \u8868\u8fbe\u5f0f\uff0c\u7528\u4e8e\u67e5\u627e DOM \u8282\u70b9\u3002<\/li>\n\n\n\n<li><strong><code>text<\/code> \u65b9\u6cd5<\/strong>\uff1a\u63d0\u53d6\u6240\u6709\u5339\u914d\u8282\u70b9\u7684\u6587\u672c\u5185\u5bb9\u3002<\/li>\n\n\n\n<li><strong><code>firstText<\/code> \u65b9\u6cd5<\/strong>\uff1a\u63d0\u53d6\u7b2c\u4e00\u4e2a\u5339\u914d\u8282\u70b9\u7684\u6587\u672c\u5185\u5bb9\u3002<\/li>\n\n\n\n<li><strong><code>html<\/code> \u65b9\u6cd5<\/strong>\uff1a\u83b7\u53d6\u6216\u8bbe\u7f6e\u8282\u70b9\u7684 HTML \u5185\u5bb9\u3002<\/li>\n\n\n\n<li><strong><code>attr<\/code> \u65b9\u6cd5<\/strong>\uff1a\u83b7\u53d6\u6307\u5b9a\u5c5e\u6027\u7684\u503c\u3002<\/li>\n\n\n\n<li><strong><code>append<\/code> \u65b9\u6cd5<\/strong>\uff1a\u5411\u5339\u914d\u7684\u8282\u70b9\u8ffd\u52a0\u65b0\u7684 HTML\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u6269\u5c55\u4e0e\u4f18\u5316<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>CSS \u9009\u62e9\u5668\u652f\u6301<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff08\u5982 Symfony \u7684 CssSelector\uff09\u5c06 CSS \u9009\u62e9\u5668\u8f6c\u6362\u4e3a XPath \u8868\u8fbe\u5f0f\u3002<\/li>\n\n\n\n<li><strong>\u4e8b\u4ef6\u7ed1\u5b9a<\/strong>\uff1aPHP \u662f\u670d\u52a1\u7aef\u8bed\u8a00\uff0c\u65e0\u6cd5\u76f4\u63a5\u5b9e\u73b0\u7c7b\u4f3c jQuery \u7684\u4e8b\u4ef6\u7ed1\u5b9a\u529f\u80fd\u3002\u5982\u679c\u9700\u8981\u5ba2\u6237\u7aef\u4ea4\u4e92\uff0c\u5efa\u8bae\u4f7f\u7528 JavaScript \u6216 jQuery\u3002<\/li>\n\n\n\n<li><strong>\u6027\u80fd\u4f18\u5316<\/strong>\uff1a\u5bf9\u4e8e\u590d\u6742\u7684 DOM \u64cd\u4f5c\uff0c\u5c3d\u91cf\u51cf\u5c11\u91cd\u590d\u67e5\u8be2\uff0c\u7f13\u5b58\u7ed3\u679c\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u7b80\u5355\u5b9e\u73b0\u4e8c<\/h2>\n\n\n\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u8ba9 PHP \u7684 DOM \u64cd\u4f5c\u66f4\u52a0\u63a5\u8fd1 jQuery \u7684\u98ce\u683c\uff0c\u63d0\u5347\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u5f00\u53d1\u6548\u7387\u3002<\/p>\n\n\n\n<p>\u4e3a\u4e86\u66f4\u5b8c\u6574\u5730\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u5c01\u88c5\u7684 <code>Query<\/code> \u7c7b\uff0c\u6211\u5c06\u63d0\u4f9b\u6db5\u76d6\u5404\u79cd\u7c7b\u578b\u64cd\u4f5c\u7684\u793a\u4f8b\u4ee3\u7801\u3002\u8fd9\u4e9b\u793a\u4f8b\u5c06\u5305\u62ec\u6587\u672c\u63d0\u53d6\u3001\u5c5e\u6027\u64cd\u4f5c\u3001HTML \u4fee\u6539\u3001\u8282\u70b9\u6dfb\u52a0\u4e0e\u5220\u9664\u7b49\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b8c\u6574\u793a\u4f8b\u4ee3\u7801<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nclass Query {\n    private $dom;\n    private $xpath;\n    private $nodes = &#91;];\n\n    public function __construct($html = null) {\n        $this-&gt;dom = new DOMDocument();\n        libxml_use_internal_errors(true); \/\/ \u9632\u6b62 HTML \u89e3\u6790\u9519\u8bef\u629b\u51fa\u5f02\u5e38\n        if ($html) {\n            $this-&gt;dom-&gt;loadHTML($html);\n        }\n        $this-&gt;xpath = new DOMXPath($this-&gt;dom);\n        libxml_clear_errors();\n    }\n\n    \/\/ \u67e5\u627e\u8282\u70b9\n    public function find($selector) {\n        $this-&gt;nodes = $this-&gt;xpath-&gt;query($selector);\n        return $this; \/\/ \u652f\u6301\u94fe\u5f0f\u8c03\u7528\n    }\n\n    \/\/ \u83b7\u53d6\u6240\u6709\u5339\u914d\u8282\u70b9\u7684\u6587\u672c\u5185\u5bb9\n    public function text() {\n        $texts = &#91;];\n        foreach ($this-&gt;nodes as $node) {\n            $texts&#91;] = $node-&gt;nodeValue;\n        }\n        return $texts;\n    }\n\n    \/\/ \u83b7\u53d6\u7b2c\u4e00\u4e2a\u5339\u914d\u8282\u70b9\u7684\u6587\u672c\u5185\u5bb9\n    public function firstText() {\n        if ($this-&gt;nodes-&gt;length &gt; 0) {\n            return $this-&gt;nodes-&gt;item(0)-&gt;nodeValue;\n        }\n        return null;\n    }\n\n    \/\/ \u83b7\u53d6\u6216\u8bbe\u7f6e HTML \u5185\u5bb9\n    public function html($newHtml = null) {\n        if ($newHtml !== null) {\n            foreach ($this-&gt;nodes as $node) {\n                \/\/ \u6e05\u7a7a\u8282\u70b9\u73b0\u6709\u5185\u5bb9\n                while ($node-&gt;firstChild) {\n                    $node-&gt;removeChild($node-&gt;firstChild);\n                }\n                \/\/ \u6dfb\u52a0\u65b0\u7684 HTML \u5185\u5bb9\n                $fragment = $this-&gt;dom-&gt;createDocumentFragment();\n                $fragment-&gt;appendXML($newHtml);\n                $node-&gt;appendChild($fragment);\n            }\n            return $this;\n        }\n        $htmls = &#91;];\n        foreach ($this-&gt;nodes as $node) {\n            $htmls&#91;] = $this-&gt;dom-&gt;saveHTML($node);\n        }\n        return $htmls;\n    }\n\n    \/\/ \u83b7\u53d6\u6216\u8bbe\u7f6e\u5c5e\u6027\u503c\n    public function attr($attribute, $value = null) {\n        if ($value !== null) {\n            foreach ($this-&gt;nodes as $node) {\n                $node-&gt;setAttribute($attribute, $value);\n            }\n            return $this;\n        }\n        $attrs = &#91;];\n        foreach ($this-&gt;nodes as $node) {\n            if ($node-&gt;hasAttribute($attribute)) {\n                $attrs&#91;] = $node-&gt;getAttribute($attribute);\n            }\n        }\n        return $attrs;\n    }\n\n    \/\/ \u5411\u5339\u914d\u8282\u70b9\u8ffd\u52a0\u5185\u5bb9\n    public function append($html) {\n        foreach ($this-&gt;nodes as $node) {\n            $fragment = $this-&gt;dom-&gt;createDocumentFragment();\n            $fragment-&gt;appendXML($html);\n            $node-&gt;appendChild($fragment);\n        }\n        return $this;\n    }\n\n    \/\/ \u5220\u9664\u5339\u914d\u7684\u8282\u70b9\n    public function remove() {\n        foreach ($this-&gt;nodes as $node) {\n            if ($node-&gt;parentNode) {\n                $node-&gt;parentNode-&gt;removeChild($node);\n            }\n        }\n        return $this;\n    }\n\n    \/\/ \u8f93\u51fa\u5b8c\u6574\u7684 HTML\n    public function saveHTML() {\n        return $this-&gt;dom-&gt;saveHTML();\n    }\n}\n\n\/\/ \u793a\u4f8b HTML\n$html = &lt;&lt;&lt;HTML\n&lt;html&gt;\n&lt;body&gt;\n    &lt;div class=\"container\"&gt;\n        &lt;p id=\"para1\" class=\"text\"&gt;Hello, World!&lt;\/p&gt;\n        &lt;p id=\"para2\" class=\"text\"&gt;This is a test.&lt;\/p&gt;\n        &lt;a href=\"https:<a href=\"https:\/\/www.zhaozhao123.cn\/skin\/go?url=aHR0cDovL2V4YW1wbGUuY29t\" rel=\"noopener noreferrer nofollow\">\/\/example.com<\/a>\" title=\"Example Link\"&gt;Click Me&lt;\/a&gt;\n    &lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\nHTML;\n\n$query = new Query($html);\n\n\/\/ \u793a\u4f8b 1: \u67e5\u627e\u6240\u6709 p \u6807\u7b7e\u5e76\u83b7\u53d6\u5b83\u4eec\u7684\u6587\u672c\u5185\u5bb9\n$texts = $query-&gt;find('<a href=\"https:\/\/www.zhaozhao123.cn\/skin\/go?url=aHR0cDovL3A=\" rel=\"noopener noreferrer nofollow\">\/\/p<\/a>')-&gt;text();\necho \"All paragraph texts:n\";\nprint_r($texts);\n\n\/\/ \u793a\u4f8b 2: \u67e5\u627e\u7b2c\u4e00\u4e2a p \u6807\u7b7e\u7684\u6587\u672c\u5185\u5bb9\n$firstText = $query-&gt;find('<a href=\"https:\/\/www.zhaozhao123.cn\/skin\/go?url=aHR0cDovL3A=\" rel=\"noopener noreferrer nofollow\">\/\/p<\/a>')-&gt;firstText();\necho \"First paragraph text: $firstTextn\";\n\n\/\/ \u793a\u4f8b 3: \u83b7\u53d6\u6240\u6709 p \u6807\u7b7e\u7684 HTML\n$htmls = $query-&gt;find('<a href=\"https:\/\/www.zhaozhao123.cn\/skin\/go?url=aHR0cDovL3A=\" rel=\"noopener noreferrer nofollow\">\/\/p<\/a>')-&gt;html();\necho \"All paragraph HTMLs:n\";\nprint_r($htmls);\n\n\/\/ \u793a\u4f8b 4: \u4fee\u6539\u7b2c\u4e00\u4e2a p \u6807\u7b7e\u7684\u5185\u5bb9\n$query-&gt;find('<a href=\"https:\/\/www.zhaozhao123.cn\/skin\/go?url=aHR0cDovL3A=\" rel=\"noopener noreferrer nofollow\">\/\/p<\/a>&#91;@id=\"para1\"]')-&gt;html('&lt;strong&gt;New Content&lt;\/strong&gt;');\necho \"Modified HTML:n\";\necho $query-&gt;saveHTML();\n\n\/\/ \u793a\u4f8b 5: \u83b7\u53d6\u6240\u6709\u94fe\u63a5\u7684 href \u5c5e\u6027\n$hrefs = $query-&gt;find('<a href=\"http:\/\/\">\/\/<\/a>')-&gt;attr('href');\necho \"All href attributes:n\";\nprint_r($hrefs);\n\n\/\/ \u793a\u4f8b 6: \u4fee\u6539\u6240\u6709\u94fe\u63a5\u7684 title \u5c5e\u6027\n$query-&gt;find('<a href=\"http:\/\/\">\/\/<\/a>')-&gt;attr('title', 'Updated Title');\necho \"Updated HTML with new title attribute:n\";\necho $query-&gt;saveHTML();\n\n\/\/ \u793a\u4f8b 7: \u5411\u5bb9\u5668 div \u4e2d\u8ffd\u52a0\u65b0\u7684\u5185\u5bb9\n$query-&gt;find('<a href=\"https:\/\/www.zhaozhao123.cn\/skin\/go?url=aHR0cDovL2Rpdg==\" rel=\"noopener noreferrer nofollow\">\/\/div<\/a>&#91;@class=\"container\"]')-&gt;append('&lt;span&gt;Appended Text&lt;\/span&gt;');\necho \"HTML after appending content:n\";\necho $query-&gt;saveHTML();\n\n\/\/ \u793a\u4f8b 8: \u5220\u9664\u7b2c\u4e8c\u4e2a p \u6807\u7b7e\n$query-&gt;find('<a href=\"https:\/\/www.zhaozhao123.cn\/skin\/go?url=aHR0cDovL3A=\" rel=\"noopener noreferrer nofollow\">\/\/p<\/a>&#91;@id=\"para2\"]')-&gt;remove();\necho \"HTML after removing second paragraph:n\";\necho $query-&gt;saveHTML();<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b\u8f93\u51fa\u89e3\u91ca<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b 1: \u83b7\u53d6\u6240\u6709\u6bb5\u843d\u6587\u672c<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>All paragraph texts:\nArray\n(\n    &#91;0] =&gt; Hello, World!\n    &#91;1] =&gt; This is a test.\n)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b 2: \u83b7\u53d6\u7b2c\u4e00\u4e2a\u6bb5\u843d\u6587\u672c<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>First paragraph text: Hello, World!<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b 3: \u83b7\u53d6\u6240\u6709\u6bb5\u843d\u7684 HTML<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>All paragraph HTMLs:\nArray\n(\n    &#91;0] =&gt; &lt;p id=\"para1\" class=\"text\"&gt;Hello, World!&lt;\/p&gt;\n    &#91;1] =&gt; &lt;p id=\"para2\" class=\"text\"&gt;This is a test.&lt;\/p&gt;\n)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b 4: \u4fee\u6539\u7b2c\u4e00\u4e2a\u6bb5\u843d\u7684\u5185\u5bb9<\/h4>\n\n\n\n<p>\u4fee\u6539\u540e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;p id=\"para1\" class=\"text\"&gt;&lt;strong&gt;New Content&lt;\/strong&gt;&lt;\/p&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b 5: \u83b7\u53d6\u6240\u6709\u94fe\u63a5\u7684 <code>href<\/code> \u5c5e\u6027<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>All href attributes:\nArray\n(\n    &#91;0] =&gt; <a href=\"https:\/\/www.zhaozhao123.cn\/skin\/go?url=aHR0cHM6Ly9leGFtcGxlLmNvbQ==\" rel=\"noopener noreferrer nofollow\">https:\/\/example.com<\/a>\n)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b 6: \u4fee\u6539\u6240\u6709\u94fe\u63a5\u7684 <code>title<\/code> \u5c5e\u6027<\/h4>\n\n\n\n<p>\u4fee\u6539\u540e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;a href=\"https:<a href=\"https:\/\/www.zhaozhao123.cn\/skin\/go?url=aHR0cDovL2V4YW1wbGUuY29t\" rel=\"noopener noreferrer nofollow\">\/\/example.com<\/a>\" title=\"Updated Title\"&gt;Click Me&lt;\/a&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b 7: \u5411\u5bb9\u5668\u4e2d\u8ffd\u52a0\u5185\u5bb9<\/h4>\n\n\n\n<p>\u8ffd\u52a0\u540e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div class=\"container\"&gt;\n    ...\n    &lt;span&gt;Appended Text&lt;\/span&gt;\n&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b 8: \u5220\u9664\u7b2c\u4e8c\u4e2a\u6bb5\u843d<\/h4>\n\n\n\n<p>\u5220\u9664\u540e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div class=\"container\"&gt;\n    &lt;p id=\"para1\" class=\"text\"&gt;&lt;strong&gt;New Content&lt;\/strong&gt;&lt;\/p&gt;\n    &lt;a href=\"https:<a href=\"https:\/\/www.zhaozhao123.cn\/skin\/go?url=aHR0cDovL2V4YW1wbGUuY29t\" rel=\"noopener noreferrer nofollow\">\/\/example.com<\/a>\" title=\"Updated Title\"&gt;Click Me&lt;\/a&gt;\n    &lt;span&gt;Appended Text&lt;\/span&gt;\n&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<p>\u901a\u8fc7\u4e0a\u8ff0\u793a\u4f8b\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u5982\u4f55\u4f7f\u7528\u5c01\u88c5\u7684 <code>Query<\/code> \u7c7b\u6765\u5b9e\u73b0\u7c7b\u4f3c jQuery \u7684\u529f\u80fd\u3002\u8fd9\u4e2a\u7c7b\u652f\u6301\u4ee5\u4e0b\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u67e5\u627e\u8282\u70b9<\/strong>\uff08<code>find<\/code>\uff09<\/li>\n\n\n\n<li><strong>\u83b7\u53d6\/\u8bbe\u7f6e\u6587\u672c<\/strong>\uff08<code>text<\/code> \u548c <code>firstText<\/code>\uff09<\/li>\n\n\n\n<li><strong>\u83b7\u53d6\/\u8bbe\u7f6e HTML<\/strong>\uff08<code>html<\/code>\uff09<\/li>\n\n\n\n<li><strong>\u83b7\u53d6\/\u8bbe\u7f6e\u5c5e\u6027<\/strong>\uff08<code>attr<\/code>\uff09<\/li>\n\n\n\n<li><strong>\u8ffd\u52a0\u5185\u5bb9<\/strong>\uff08<code>append<\/code>\uff09<\/li>\n\n\n\n<li><strong>\u5220\u9664\u8282\u70b9<\/strong>\uff08<code>remove<\/code>\uff09<\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u79cd\u5c01\u88c5\u65b9\u5f0f\u4e0d\u4ec5\u8ba9\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u6613\u8bfb\uff0c\u8fd8\u63d0\u9ad8\u4e86\u5f00\u53d1\u6548\u7387\u3002\u5982\u679c\u4f60\u9700\u8981\u8fdb\u4e00\u6b65\u6269\u5c55\u529f\u80fd\uff0c\u53ef\u4ee5\u7ee7\u7eed\u6dfb\u52a0\u66f4\u591a\u65b9\u6cd5\uff0c\u4f8b\u5982\u4e8b\u4ef6\u7ed1\u5b9a\uff08\u5c3d\u7ba1 PHP \u662f\u670d\u52a1\u7aef\u8bed\u8a00\uff0c\u65e0\u6cd5\u76f4\u63a5\u5904\u7406\u5ba2\u6237\u7aef\u4e8b\u4ef6\uff09\u3002<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u5355\u5b9e\u73b0\u4e00 \u5728 PHP \u4e2d\u4f7f\u7528 DOMXPath \u8fdb\u884c DOM \u64cd\u4f5c\u65f6\uff0c\u5176\u529f\u80fd\u7c7b\u4f3c\u4e8e jQuery \u7684\u9009\u62e9\u5668\u64cd\u4f5c\uff0c\u4f46\u8bed\u6cd5\u548c\u7528\u6cd5\u4e0a\u5b58\u5728\u660e\u663e\u5dee\u5f02\u3002\u5982\u679c\u4f60\u5e0c\u671b\u5c06 DOMXPath \u7684\u83b7\u53d6\u65b9\u5f0f\u8f6c\u53d8\u4e3a\u7c7b\u4f3c jQuery \u7684\u98ce\u683c\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c01\u88c5\u4e00\u4e2a\u7c7b\u6216\u51fd\u6570\u6765\u5b9e\u73b0\u66f4\u7b80\u6d01\u7684\u94fe\u5f0f\u8c03\u7528\u548c\u7c7b\u4f3c jQuery \u7684 API\u3002 \u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5b9e\u73b0..<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"tuisongtax":[],"class_list":["post-44","post","type-post","status-publish","format-standard","hentry","category-jswz"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/posts\/44","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/comments?post=44"}],"version-history":[{"count":0,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/posts\/44\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/media?parent=44"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/categories?post=44"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=44"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=44"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}