gpt4 book ai didi

javascript - 如何从另一个文件中获取html元素值

转载 作者:行者123 更新时间:2023-11-30 12:04:07 25 4
gpt4 key购买 nike

我有两个 html 文件:

index.html

<!DOCTYPE html>
<html>
<head>
<script src="jquery.min.js"></script>
<script>
$(document).ready(function(){

$.get( "target.html", function( data ) {
var data = $(data);
var elem = $(data.find('#tst'));
console.log(elem);
});


});
</script>
</head>
<body>

<p> This an p tag</p>

</body>
</html>

目标.html

<!DOCTYPE html>
<html>
<head>
<script src="jquery.min.js"></script>
<script>
$(document).ready(function(){

});
</script>
</head>
<body>

<p> This an p tag</p>
<h1 id="tst"> Test </h1>
</body>
</html>

我希望 $('#tst') 的值从 target.html 到 index.html 。我如何实现它?

两个文件都在同一个基本目录中。 $("#tst") 值也将是静态的。

当我执行 console.log($(data)) 我得到这个对象

[text, script, text, script, text, p, text, h1#tst, text]
0: text
1: script
2: text
3: script
4: text
5: p
6: text
7: h1#tst
accessKey: ""align: ""attributes: NamedNodeMap0: idlength: 1__proto__: NamedNodeMapbaseURI: nullchildElementCount: 0childNodes: NodeList[1]children: HTMLCollection[0]classList: DOMTokenList[0]className: ""clientHeight: 0clientLeft: 0clientTop: 0clientWidth: 0contentEditable: "inherit"dataset: DOMStringMapdir: ""draggable: falsefirstChild: textfirstElementChild: nullhidden: falseid: "tst"innerHTML: " Test "innerText: " Test "isContentEditable: falselang: ""lastChild: textlastElementChild: nulllocalName: "h1"namespaceURI: "http://www.w3.org/1999/xhtml"nextElementSibling: nullnextSibling: textnodeName: "H1"nodeType: 1nodeValue: nulloffsetHeight: 0offsetLeft: 0offsetParent: nulloffsetTop: 0offsetWidth: 0onabort: nullonautocomplete: nullonautocompleteerror: nullonbeforecopy: nullonbeforecut: nullonbeforepaste: nullonblur: nulloncancel: nulloncanplay: nulloncanplaythrough: nullonchange: nullonclick: nullonclose: nulloncontextmenu: nulloncopy: nulloncuechange: nulloncut: nullondblclick: nullondrag: nullondragend: nullondragenter: nullondragleave: nullondragover: nullondragstart: nullondrop: nullondurationchange: nullonemptied: nullonended: nullonerror: nullonfocus: nulloninput: nulloninvalid: nullonkeydown: nullonkeypress: nullonkeyup: nullonload: nullonloadeddata: nullonloadedmetadata: nullonloadstart: nullonmousedown: nullonmouseenter: nullonmouseleave: nullonmousemove: nullonmouseout: nullonmouseover: nullonmouseup: nullonmousewheel: nullonpaste: nullonpause: nullonplay: nullonplaying: nullonprogress: nullonratechange: nullonreset: nullonresize: nullonscroll: nullonsearch: nullonseeked: nullonseeking: nullonselect: nullonselectstart: nullonshow: nullonstalled: nullonsubmit: nullonsuspend: nullontimeupdate: nullontoggle: nullonvolumechange: nullonwaiting: nullonwebkitfullscreenchange: nullonwebkitfullscreenerror: nullonwheel: nullouterHTML: "<h1 id="tst"> Test </h1>"outerText: " Test "ownerDocument: documentparentElement: nullparentNode: document-fragmentprefix: nullpreviousElementSibling: ppreviousSibling: textscrollHeight: 0scrollLeft: 0scrollTop: 0scrollWidth: 0shadowRoot: nullspellcheck: truestyle: CSSStyleDeclarationtabIndex: -1tagName: "H1"textContent: " Test "title: ""translate: truewebkitdropzone: ""__proto__: HTMLHeadingElement
8: textlength: 9__proto__: n[0]

当我执行 console.log(elem) 时,我得到了这个对象

n.fn.init[0]
context: undefined
length: 0
selector: "#tst"
__proto__: n[0]

最佳答案

Use .find() to get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element

试试这个:

$.get("target.html", function(data) {
var data = $(data);
var elem = data.find('#tst');
});

编辑:

由于所有直接子元素都由$(data)返回,.find()方法在这里不适用,因为我们不这样做有 parent 选择器。

创建一个dummy div 并将这个div 的html 设置为data 这样我们就可以使用DummyDiv.find()

试试这个:

$(document).ready(function() {
$.get("target.html", function(data) {
var data = $('<div>', {
html: data
});
var elem = data.find('#tst');
});
});

关于javascript - 如何从另一个文件中获取html元素值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35763949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com