【问题标题】:Use jsonified html as actual html in React在 React 中使用 jsonified html 作为实际的 html
【发布时间】:2019-10-13 08:47:47
【问题描述】:

我正在使用维基百科 API 来调用来自不同搜索的信息。

我是这样称呼它的:

const api = `${proxy}http://en.wikipedia.org/w/api.php?action=parse&format=json&section=0&prop=text&page=${this.state.value}`;

然后当我把它拿回来时,我对 HTML 很感兴趣。我通过了:

jsonObject.parse.text["*"]

然后我想用它来创建维基百科页面的副本。这就是我尝试重新创建维基百科页面的方式:

ReactDOM.render(jsonObject.parse.text["*"], document.getElementById('root'))

它没有按预期工作。谁能解释为什么?结果页面只有一个长字符串。结果是这样的:

<div class="mw-parser-output"><div class="shortdescription nomobile noexcerpt noprint searchaux" style="display:none">A four-wheeled motor vehicle used to transport people</div> <div role="note" class="hatnote navigation-not-searchable">For broader coverage of this topic, see <a href="/wiki/Motor_vehicle" title="Motor vehicle">Motor vehicle</a>.</div> <div role="note" class="hatnote navigation-not-searchable">For other uses, see <a href="/wiki/Car_(disambiguation)" class="mw-disambig" title="Car (disambiguation)">Car (disambiguation)</a> and <a href="/wiki/Automobile_(disambiguation)" class="mw-disambig" title="Automobile (disambiguation)">Automobile (disambiguation)</a>.</div> <p class="mw-empty-elt"> </p> <table class="infobox" style="width:22em"><tbody><tr><th colspan="2" style="text-align:center;font-size:125%;font-weight:bold">Car</th></tr><tr><td colspan="2" style="text-align:center"><a href="/wiki/File:401_Gridlock.jpg" class="image"><img alt="401 Gridlock.jpg" src="//upload.wikimedia.org/wikipedia/commons/thumb/5/5d/401_Gridlock.jpg/300px-401_Gridlock.jpg" decoding="async" width="300" height="200" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/5/5d/401_Gridlock.jpg/450px-401_Gridlock.jpg 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/5/5d/401_Gridlock.jpg/600px-401_Gridlock.jpg 2x" data-file-width="1600" data-file-height="1066" /></a><div>Cars and <a href="/wiki/Truck" title="Truck">trucks</a> driving on <a href="/wiki/Ontario_Highway_401" title="Ontario Highway 401">Highway 401</a> in <a href="/wiki/Ontario" title="Ontario">Ontario</a>, Canada</div></td></tr><tr><th scope="row">Classification</th><td>Vehicle</td></tr><tr><th scope="row">Industry</th><td>Various</td></tr><tr><th scope="row">Application</th><td><a href="/wiki/Transportation" class="mw-redirect" title="Transportation">Transportation</a></td></tr><tr><th scope="row">Fuel source</th><td><a href="/wiki/Gasoline" title="Gasoline">Gasoline</a>, <a href="/wiki/Diesel_fuel" title="Diesel fuel">diesel</a>, <a href="/wiki/Natural_gas" title="Natural gas">natural gas</a>, <a href="/wiki/Electric_car" title="Electric car">electric</a>, <a href="/wiki/Hydrogen" title="Hydrogen">hydrogen</a>, <a href="/wiki/Solar_energy" title="Solar energy">solar</a>, <a href="/wiki/Vegetable_oil" title="Vegetable oil">vegetable oil</a></td></tr><tr><th scope="row">Powered</th><td>Yes</td></tr><tr><th scope="row">Self-propelled</th><td>Yes</td></tr><tr><th scope="row">Wheels</th><td>3–4</td></tr><tr><th scope="row">Axles</th><td>2</td></tr><tr><th scope="row">Inventor</th><td><a href="/wiki/Karl_Benz" title="Karl Benz">Karl Benz</a><sup id="cite_ref-stein_1-0" class="reference"><a href="#cite_note-stein-1">[1]</a></sup></td></tr></tbody></table> <p>A <b>car</b> (or <b>automobile</b>) is a wheeled <a href="/wiki/Motor_vehicle" title="Motor vehicle">motor vehicle</a> used for <a href="/wiki/Transportation" class="mw-redirect" title="Transportation">transportation</a>. Most definitions of <i>car</i> say they run primarily on roads, seat one to eight people, have four <a href="/wiki/Tyre_(wheel)" class="mw-redirect" title="Tyre (wheel)">tires</a>, and mainly transport people rather than goods.<sup id="cite_ref-2" class="reference"><a href="#cite_note-2">[2]</a></sup><sup id="cite_ref-OEDmotrcar_3-0" class="reference"><a href="#cite_note-OEDmotrcar-3">[3]</a></sup> </p><p>Cars came into global use during the 20th century, and <a href="/wiki/Developed_country" title="Developed country">developed economies</a> depend on them. The year 1886 is regarded as the birth year of the modern car when German inventor <a href="/wiki/Karl_Benz" title="Karl Benz">Karl Benz</a> patented his <a href="/wiki/Benz_Patent-Motorwagen" title="Benz Patent-Motorwagen">Benz Patent-Motorwagen</a>. Cars became widely available in the early 20th century. One of the first cars accessible to the masses was the 1908 <a href="/wiki/Ford_Model_T" title="Ford Model T">Model T</a>, an American car manufactured by the <a href="/wiki/Ford_Motor_Company" title="Ford Motor Company">Ford Motor Company</a>. Cars were rapidly adopted in the US, where they replaced <a href="/wiki/Draft_animal" class="mw-redirect" title="Draft animal">animal-drawn</a> <a href="/wiki/Carriage" title="Carriage">carriages</a> and carts, but took much longer to be accepted in Western Europe and other parts of the world.<sup class="noprint Inline-Template Template-Fact" style="white-space:nowrap;">[<i><a href="/wiki/Wikipedia:Citation_needed" title="Wikipedia:Citation needed"><span title="This claim needs references to reliable sources. (August 2019)">citation needed</span></a></i>]</sup> </p><p>Cars have controls for driving, parking, passenger comfort, and a variety of lights. Over the decades, additional features and controls have been added to vehicles, making them progressively more complex, but also more reliable and easier to operate.<sup class="noprint Inline-Template Template-Fact" style="white-space:nowrap;">[<i><a href="/wiki/Wikipedia:Citation_needed" title="Wikipedia:Citation needed"><span title="This claim needs references to reliable sources. (August 2019)">citation needed</span></a></i>]</sup> These include rear reversing cameras, <a href="/wiki/Automobile_air_conditioning" title="Automobile air conditioning">air conditioning</a>, <a href="/wiki/Automotive_navigation_system" title="Automotive navigation system">navigation systems</a>, and <a href="/wiki/In-car_entertainment" title="In-car entertainment">in-car entertainment</a>. Most cars in use in the 2010s are propelled by an <a href="/wiki/Internal_combustion_engine" title="Internal combustion engine">internal combustion engine</a>, fueled by the <a href="/wiki/Combustion" title="Combustion">combustion</a> of <a href="/wiki/Fossil_fuel" title="Fossil fuel">fossil fuels</a>. <a href="/wiki/Electric_car" title="Electric car">Electric cars</a>, which were invented early in the <a href="/wiki/History_of_the_automobile" title="History of the automobile">history of the car</a>, became commercially available in the 2000s and have the potential to cost less to buy than gasoline cars in the early 2020s.<sup id="cite_ref-4" class="reference"><a href="#cite_note-4">[4]</a></sup> </p><p>There are costs and benefits to car use. The costs to the individual include acquiring the vehicle, interest payments (if the car is financed), repairs and <a href="/wiki/Auto_maintenance" class="mw-redirect" title="Auto maintenance">maintenance</a>, fuel, <a href="/wiki/Depreciation" title="Depreciation">depreciation</a>, driving time, <a href="/wiki/Parking_fee" class="mw-redirect" title="Parking fee">parking fees</a>, taxes, and insurance.<sup id="cite_ref-racv_5-0" class="reference"><a href="#cite_note-racv-5">[5]</a></sup> The costs to society include <a href="/wiki/Maintaining_road" class="mw-redirect" title="Maintaining road">maintaining roads</a>, <a href="/wiki/Land_use" title="Land use">land use</a>, <a href="/wiki/Road_congestion" class="mw-redirect" title="Road congestion">road congestion</a>, <a href="/wiki/Air_pollution" title="Air pollution">air pollution</a>, <a href="/wiki/Public_health" title="Public health">public health</a>, health care, and disposing of the vehicle at the end of its life. <a href="/wiki/Traffic_collisions" class="mw-redirect" title="Traffic collisions">Traffic collisions</a> are the largest cause of injury-related deaths worldwide.<sup id="cite_ref-who_stats_6-0" class="reference"><a href="#cite_note-who_stats-6">[6]</a></sup> </p><p> The personal benefits include on-demand transportation, mobility, independence, and convenience.<sup id="cite_ref-setright_7-0" class="reference"><a href="#cite_note-setright-7">[7]</a></sup> The societal benefits include economic benefits, such as job and wealth creation from the <a href="/wiki/Automotive_industry" title="Automotive industry">automotive industry</a>, transportation provision, societal well-being from leisure and travel opportunities, and revenue generation from the <a href="/wiki/Category:Vehicle_taxes" title="Category:Vehicle taxes">taxes</a>. People's ability to move flexibly from place to place has far-reaching implications for the nature of societies.<sup id="cite_ref-parking_8-0" class="reference"><a href="#cite_note-parking-8">[8]</a></sup> There are around 1 billion cars in use worldwide. The numbers are increasing rapidly, especially in <a href="/wiki/Automotive_industry_in_China" title="Automotive industry in China">China</a>, <a href="/wiki/Automotive_industry_in_India" title="Automotive industry in India">India</a> and other <a href="/wiki/Newly_industrialized_country" title="Newly industrialized country">newly industrialized countries</a>.<sup id="cite_ref-plunkettresearch.com_9-0" class="reference"><a href="#cite_note-plunkettresearch.com-9">[9]</a></sup></p><div class="mw-references-wrap"><ol class="references"> <li id="cite_note-stein-1"><span class="mw-cite-backlink"><b><a href="#cite_ref-stein_1-0">^</a></b></span> <span class="error mw-ext-cite-error" lang="en" dir="ltr">Cite error: The named reference <code>stein</code> was invoked but never defined (see the <a href="/wiki/Help:Cite_errors/Cite_error_references_no_text" title="Help:Cite errors/Cite error references no text">help page</a>). </span></li> <li id="cite_note-2"><span class="mw-cite-backlink"><b><a href="#cite_ref-2">^</a></b></span> <span class="reference-text"><cite class="citation book">Fowler, H.W.; Fowler, F.G., eds. (1976). <i>Pocket Oxford Dictionary</i>. Oxford University Press. <a href="/wiki/International_Standard_Book_Number" title="International Standard Book Number">ISBN</a> <a href="/wiki/Special:BookSources/978-0198611134" title="Special:BookSources/978-0198611134"><bdi>978-0198611134</bdi></a>.</cite><span title="ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=book&rft.btitle=Pocket+Oxford+Dictionary&rft.pub=Oxford+University+Press&rft.date=1976&rft.isbn=978-0198611134&rfr_id=info%3Asid%2Fen.wikipedia.org%3ACar" class="Z3988"></span><style data-mw-deduplicate="TemplateStyles:r886058088">.mw-parser-output cite.citation{font-style:inherit}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .citation .cs1-lock-free a{background:url("//upload.wikimedia.org/wikipedia/commons/thumb/6/65/Lock-green.svg/9px-Lock-green.svg.png")no-repeat;background-position:right .1em center}.mw-parser-output .citation .cs1-lock-limited a,.mw-parser-output .citation .cs1-lock-registration a{background:url("//upload.wikimedia.org/wikipedia/commons/thumb/d/d6/Lock-gray-alt-2.svg/9px-Lock-gray-alt-2.svg.png")no-repeat;background-position:right .1em center}.mw-parser-output .citation .cs1-lock-subscription a{background:url("//upload.wikimedia.org/wikipedia/commons/thumb/a/aa/Lock-red-alt-2.svg/9px-Lock-red-alt-2.svg.png")no-repeat;background-position:right .1em center}.mw-parser-output .cs1-subscription,.mw-parser-output .cs1-registration{color:#555}.mw-parser-output .cs1-subscription span,.mw-parser-output .cs1-registration span{border-bottom:1px dotted;cursor:help}.mw-parser-output .cs1-ws-icon a{background:url("//upload.wikimedia.org/wikipedia/commons/thumb/4/4c/Wikisource-logo.svg/12px-Wikisource-logo.svg.png")no-repeat;background-position:right .1em center}.mw-parser-output code.cs1-code{color:inherit;background:inherit;border:inherit;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;font-size:100%}.mw-parser-output .cs1-visible-error{font-size:100%}.mw-parser-output .cs1-maint{display:none;color:#33aa33;margin-left:0.3em}.mw-parser-output .cs1-subscription,.mw-parser-output .cs1-registration,.mw-parser-output .cs1-format{font-size:95%}.mw-parser-output .cs1-kern-left,.mw-parser-output .cs1-kern-wl-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right,.mw-parser-output .cs1-kern-wl-right{padding-right:0.2em}</style></span> </li> <li id="cite_note-OEDmotrcar-3"><span class="mw-cite-backlink"><b><a href="#cite_ref-OEDmotrcar_3-0">^</a></b></span> <span class="error mw-ext-cite-error" lang="en" dir="ltr">Cite error: The named reference <code>OEDmotrcar</code> was invoked but never defined (see the <a href="/wiki/Help:Cite_errors/Cite_error_references_no_text" title="Help:Cite errors/Cite error references no text">help page</a>). </span></li> <li id="cite_note-4"><span class="mw-cite-backlink"><b><a href="#cite_ref-4">^</a></b></span> <span class="reference-text"><cite class="citation web"><a rel="nofollow" class="external text" href="https://cleantechnica.com/2019/08/09/ev-price-parity-coming-soon-claims-vw-executive/">"EV Price Parity Coming Soon, Claims VW Executive"</a>. <i>CleanTechnica</i>. 9 August 2019<span class="reference-accessdate">. Retrieved <span class="nowrap">10 August</span> 2019</span>.</cite><span title="ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Ajournal&rft.genre=unknown&rft.jtitle=CleanTechnica&rft.atitle=EV+Price+Parity+Coming+Soon%2C+Claims+VW+Executive&rft.date=2019-08-09&rft_id=https%3A%2F%2Fcleantechnica.com%2F2019%2F08%2F09%2Fev-price-parity-coming-soon-claims-vw-executive%2F&rfr_id=info%3Asid%2Fen.wikipedia.org%3ACar" class="Z3988"></span><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886058088"/></span> </li> <li id="cite_note-racv-5"><span class="mw-cite-backlink"><b><a href="#cite_ref-racv_5-0">^</a></b></span> <span class="reference-text"><cite class="citation web"><a rel="nofollow" class="external text" href="https://web.archive.org/web/20091007121101/http://www.racv.com.au/wps/wcm/connect/racv/Internet/Primary/my%2Bcar/advice%2B_%2Binformation/vehicle%2Boperating%2Bcosts/">"Car Operating Costs"</a>. RACV. Archived from <a rel="nofollow" class="external text" href="http://www.racv.com.au/wps/wcm/connect/racv/Internet/Primary/my+car/advice+_+information/vehicle+operating+costs/">the original</a> on 7 October 2009<span class="reference-accessdate">. Retrieved <span class="nowrap">22 December</span> 2009</span>.</cite><span title="ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=unknown&rft.btitle=Car+Operating+Costs&rft.pub=RACV&rft_id=http%3A%2F%2Fwww.racv.com.au%2Fwps%2Fwcm%2Fconnect%2Fracv%2FInternet%2FPrimary%2Fmy%2Bcar%2Fadvice%2B_%2Binformation%2Fvehicle%2Boperating%2Bcosts%2F&rfr_id=info%3Asid%2Fen.wikipedia.org%3ACar" class="Z3988"></span><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886058088"/></span> </li> <li id="cite_note-who_stats-6"><span class="mw-cite-backlink"><b><a href="#cite_ref-who_stats_6-0">^</a></b></span> <span class="reference-text"><cite class="citation book">Peden, Margie; Scurfield, Richard; Sleet, David; Mohan, Dinesh; Hyder, Adnan A.; Jarawan, Eva; Mathers, Colin, eds. (2004). <a rel="nofollow" class="external text" href="http://who.int/violence_injury_prevention/publications/road_traffic/world_report/en/"><i>World report on road traffic injury prevention</i></a>. World Health Organization. <a href="/wiki/International_Standard_Book_Number" title="International Standard Book Number">ISBN</a> <a href="/wiki/Special:BookSources/92-4-156260-9" title="Special:BookSources/92-4-156260-9"><bdi>92-4-156260-9</bdi></a><span class="reference-accessdate">. Retrieved <span class="nowrap">24 June</span> 2008</span>.</cite><span title="ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=book&rft.btitle=World+report+on+road+traffic+injury+prevention&rft.pub=World+Health+Organization&rft.date=2004&rft.isbn=92-4-156260-9&rft_id=http%3A%2F%2Fwho.int%2Fviolence_injury_prevention%2Fpublications%2Froad_traffic%2Fworld_report%2Fen%2F&rfr_id=info%3Asid%2Fen.wikipedia.org%3ACar" class="Z3988"></span><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886058088"/></span> </li> <li id="cite_note-setright-7"><span class="mw-cite-backlink"><b><a href="#cite_ref-setright_7-0">^</a></b></span> <span class="error mw-ext-cite-error" lang="en" dir="ltr">Cite error: The named reference <code>setright</code> was invoked but never defined (see the <a href="/wiki/Help:Cite_errors/Cite_error_references_no_text" title="Help:Cite errors/Cite error references no text">help page</a>). </span></li> <li id="cite_note-parking-8"><span class="mw-cite-backlink"><b><a href="#cite_ref-parking_8-0">^</a></b></span> <span class="reference-text"><cite class="citation book">Jakle, John A.; Sculle, Keith A. (2004). <i>Lots of Parking: Land Use in a Car Culture</i>. University of Virginia Press. <a href="/wiki/International_Standard_Book_Number" title="International Standard Book Number">ISBN</a> <a href="/wiki/Special:BookSources/0-8139-2266-6" title="Special:BookSources/0-8139-2266-6"><bdi>0-8139-2266-6</bdi></a>.</cite><span title="ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=book&rft.btitle=Lots+of+Parking%3A+Land+Use+in+a+Car+Culture&rft.pub=University+of+Virginia+Press&rft.date=2004&rft.isbn=0-8139-2266-6&rft.aulast=Jakle&rft.aufirst=John+A.&rft.au=Sculle%2C+Keith+A.&rfr_id=info%3Asid%2Fen.wikipedia.org%3ACar" class="Z3988"></span><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886058088"/></span> </li> <li id="cite_note-plunkettresearch.com-9"><span class="mw-cite-backlink"><b><a href="#cite_ref-plunkettresearch.com_9-0">^</a></b></span> <span class="reference-text"><cite class="citation web"><a rel="nofollow" class="external text" href="https://web.archive.org/web/20110722031051/http://www.plunkettresearch.com/automobiles%20trucks%20market%20research/industry%20overview">"Automobile Industry Introduction"</a>. Plunkett Research. Archived from <a rel="nofollow" class="external text" href="http://www.plunkettresearch.com/Industries/AutomobilesTrucks/AutomobileTrends/tabid/89/Default.aspx">the original</a> on 22 July 2011.</cite><span title="ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=unknown&rft.btitle=Automobile+Industry+Introduction&rft.pub=Plunkett+Research&rft_id=http%3A%2F%2Fwww.plunkettresearch.com%2FIndustries%2FAutomobilesTrucks%2FAutomobileTrends%2Ftabid%2F89%2FDefault.aspx&rfr_id=info%3Asid%2Fen.wikipedia.org%3ACar" class="Z3988"></span><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r886058088"/></span> </li> </ol></div> <!-- NewPP limit report Parsed by mw1289 Cached time: 20191013083710 Cache expiry: 2592000 Dynamic content: false Complications: [vary‐revision‐sha1] CPU time usage: 0.332 seconds Real time usage: 0.499 seconds Preprocessor visited node count: 1184/1000000 Preprocessor generated node count: 0/1500000 Post‐expand include size: 22522/2097152 bytes Template argument size: 3602/2097152 bytes Highest expansion depth: 13/40 Expensive parser function count: 4/500 Unstrip recursion depth: 0/20 Unstrip post‐expand size: 12133/5000000 bytes Number of Wikibase entities loaded: 0/400 Lua time usage: 0.152/10.000 seconds Lua memory usage: 3.95 MB/50 MB --> <!-- Transclusion expansion time report (%,ms,calls,template) 100.00% 476.128 1 -total 18.96% 90.271 3 Template:Cite_book 11.75% 55.942 1 Template:Pp-semi-vandalism 10.35% 49.266 2 Template:Cn 9.87% 47.004 1 Template:Short_description 9.05% 43.105 1 Template:Pagetype 8.69% 41.353 2 Template:Fix 7.57% 36.060 6 Template:Broken_ref 6.32% 30.079 1 Template:Infobox_machine 5.53% 26.318 4 Template:Category_handler --> </div>

【问题讨论】:

    标签: javascript html json reactjs


    【解决方案1】:

    默认情况下,React 会阻止 javascript 插入的 HTML。如果你想解决这个问题,你需要在你的内容的父级上使用 dangerouslySetInnerHTML 属性。

    例子:

    // data from fetch
    let data;
    
    return <div dangerouslySetInnerHTML={data}></div>
    

    执行此操作时,请确保您信任 HTML 源。如果您像这样将用户生成的 HTML 添加到您的网站,您的网站将容易受到 XSS 攻击。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      • 1970-01-01
      • 1970-01-01
      • 2012-05-04
      • 1970-01-01
      • 2012-04-22
      • 2018-11-23
      相关资源
      最近更新 更多