Код: Выделить всё
под тем, что у меня есть тег.
ниже. Он ищет тег с внутренним текстом «HTML COM объекта». После того, как он обнаружил, он должен повторять через детские элементы этого ищущий теги и изменяя их внутренний текст со значением $ targeth1text.
он обнаруживает теги , но не находит теги .# Specify the path to your HTML file
$htmlFilePath = "c:\myhtml.html"
# Specify the target H1 inner text
$targetH1Text = "html COM object"
# Specify the new inner text for code tags
$newCodeText = "Modified Code Content"
# Load the HTML document
$html = New-Object -ComObject "HTMLFile"
$html.IHTMLDocument2_write((Get-Content -Path $htmlFilePath -Raw))
# Find the H1 tag with the specified inner text
$targetH1 = $html.getElementsByTagName("h1") | Where-Object { $_.innerText -eq $targetH1Text }
# If the H1 tag is found, proceed to modify code tags within its descendants
if ($targetH1) {
# Find all code tags within the target H1's descendants
$codeTags = $targetH1.getElementsByTagName("code")
# Iterate through the found code tags and modify their inner text
foreach ($codeTag in $codeTags) {
$codeTag.innerText = $newCodeText
}
# Save the modified HTML back to the file
$html.IHTMLDocument2_close() # Close the document before saving changes
Set-Content -Path $htmlFilePath -Value $html.documentElement.outerHTML
Write-Host "Code tags under '$targetH1Text' H1 modified successfully."
} else {
Write-Host "H1 tag with inner text '$targetH1Text' not found."
}
< /code>
Ниже приведен мой HTML, который я поместил в файл с именем c: \ myhtml.html < /p>
This is html COM object example
Some text
Old Code 1Код: Выделить всё
Another Code TagContent under matching h1
Код: Выделить всё
Code to ChangeПодробнее здесь: https://stackoverflow.com/questions/796 ... in-an-html