mock-browserというライブラリを使用して、Node.jsでdiv要素を生成したく以下のようにコーディングしました。
しかしconsole.log(document.getElementsByTagName('div'))
で出力するとlength: 0となっておりdivが生成されていないようです。
初めてNode.jsを使ったので大変初歩的な質問かと思いますが、是非ともご教授いただけると幸いです。
どうぞよろしくお願い致します。
javascript
1const express = require('express') 2var mockBrowser = require('mock-browser').mocks.MockBrowser 3 4global.window = mockBrowser.createWindow() 5global.document = window.document 6global.navigator = window.navigator 7global.HTMLCollection = window.HTMLCollection 8global.getComputedStyle = window.getComputedStyle 9 10const app = express() 11 12 13app.get('/', (req, res) => { 14 const div = document.createElement('div') 15 16 console.log(document.getElementsByTagName('div')) 17} 18
console.logの出力結果
HTMLCollection { [Symbol(HTMLCollection internal slots)]: { element: <ref *1> DocumentImpl { _eventListeners: [Object: null prototype], _core: [Object], _ownerDocument: [Circular *1], _childNodesList: null, _childrenList: null, _version: 4, _memoizedQueries: [Object], nodeType: 9, _parsingMode: 'html', _htmlToDom: [HtmlToDom], _implementation: [DOMImplementationImpl], _defaultView: [Window], _global: [Window], _documentElement: null, _ids: [Object: null prototype] {}, _attached: true, _currentScript: null, _cookieJar: [CookieJar], _contentType: undefined, _encoding: 'UTF-8', _URL: [Object], _origin: 'null', _location: [LocationImpl], _history: [HistoryImpl], _activeNodeIterators: [], _activeNodeIteratorsMax: 10, _referrer: '', _lastModified: '09/27/2022 20:20:54', _queue: [ResourceQueue], _customResourceLoader: undefined, _pool: [Object], _agentOptions: [Object], _strictSSL: true, _proxy: undefined, _requestManager: [RequestManager], readyState: 'complete', _lastFocusedElement: null, [Symbol(DOM SymbolTree)]: [SymbolTreeNode], [Symbol(wrapper)]: [Document] }, query: [Function (anonymous)], keys: [], length: 0, version: 4, conflictElements: [Object: null prototype] {} } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/09/28 13:42