简体   繁体   English

javascript中字符串到html的转换

[英]string to html conversion in javascript

I want to print p as a html but its printing as it is in my webpage as a string what should I do for printing as a html object in my webpage The code is given below我想将 p 打印为 html 但它在我的网页中打印为字符串我应该怎么做才能在我的网页中打印为 html 对象代码如下

    const page_nav = () => {
            var no=FilterBooks.length/20;
            var p=''
            for(var i=1;i<=no;i++){
               p=p+'<li className="page-item" onClick=book1('+i+')><a className="page-link">'+i+'</a></li>'
            }
            return (p);
    }
    const bookl = (baselimit) => {

        if (FilterBooks.length == 0) {
            return (<p> No Books Found</p>
            )
        } else if(FilterBooks.length<20){
            return( <div id="root"><Bookslist
            filterbooks={FilterBooks}
        /></div>)
        }
        else {
            return (
                <div id='bookdisplay'>
                    <div id="root"><Bookslist
                        filterbooks={FilterBooks.slice(baselimit,baselimit+20)}
                    /></div>
                    <nav aria-label="Page navigation example">
                        <ul class="pagination">
                            <li class="page-item">
                                <a class="page-link" href="#" aria-label="Previous">
                                    <span aria-hidden="true">&laquo;</span>
                                </a>
                            </li>
                            {
                                page_nav()
                            }
                            {/* {<li class="page-item"><a class="page-link" href="#">1</a></li>
                            <li class="page-item"><a class="page-link" href="#">2</a></li>
                            <li class="page-item"><a class="page-link" href="#">3</a></li>} */}
                            <li class="page-item">
                                <a class="page-link" href="#" aria-label="Next">
                                    <span aria-hidden="true">&raquo;</span>
                                </a>
                            </li>
                        </ul>
                    </nav>

                </div>)
        }

Please can someone help with this??请问有人可以帮忙吗??

You don't need to convert string to HTML, just render the JSX:您不需要将字符串转换为 HTML,只需呈现 JSX:

// Same logic as page_nav()
<>
  {[...Array(FilterBooks.length / 20).keys()].map((key) => (
    <li className="page-item" key={key} onClick={() => bookl(key+1)}>
      <a className="page-link">{key+1}</a>
    </li>
  ))}
</>

You can set innerHTML but I would not recommend as it's easy to inadvertently expose your users to a cross-site scripting (XSS) attack:您可以设置innerHTML,但我不建议这样做,因为很容易在不经意间将您的用户暴露给跨站点脚本(XSS)攻击:

<div dangerouslySetInnerHTML={{__html: '<strong>strong text</strong>'}} />

You could also try html-react-parser .您也可以尝试html-react-parser

I'd just do it the proper way with JSX.我只是用 JSX 以正确的方式来做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM