简体   繁体   English

如何添加多个元素,例如<span>,</span> <p> <span>要么</span> <div> <span>内</span> <option> <span>的标签</span> <select> <span>在ReactJs中?</span>

[英]How can I add multiple elements like <span>, <p> or <div> inside <option> tag of <select> in ReactJs?

Reactjs is not allowing to add any html tags inside option tag. Reactjs不允许在选项标签内添加任何html标签。 It only accepting one string enclosed by { } 它只接受由{}括起来的一个字符串
I want to give some fixed width to the span. 我想给跨度设置一些固定的宽度。

Tried:- 尝试过:-

1.    {<span>{country.iso_code}</span>
      <span>{country.dialing_code}</span>}

2.    <span>{country.iso_code}</span>
      <span>{country.dialing_code}</span>

3.    <Fragment><span>{country.iso_code}</span>
        <span>{country.dialing_code}</span></Fragment>

Code:- 码:-

    <select className={styles.selectCompany}
     name="country_code"
     value={this.state.personFields["country_code"]}
     onChange={this.handlePersonFieldsChange}>
     <option value="" disabled>Select</option>
     {
       this.props.countryList ?
       this.props.countryList.map((country, index) => {
       return (
         <option value={country.dialing_code} key={"code_" + index}>
          {country.iso_code +" +"+ country.dialing_code}
          //This is working line-i.e.only one string line
          {
            <span>{country.iso_code}</span>
            <span>{country.dialing_code}</span>
          }
         </option>
        )
      })
     : ''
     }
   </select>

I want to apply fixed width to the spans. 我想将固定宽度应用于跨度。

To make the option value to fixed width, I guess we should trim/manipulate the string using JS, instead of the styling. 为了将选项值设置为固定宽度,我想我们应该使用JS而不是样式来修剪/操纵字符串。

As you already noticed, the <span> is not allowed inside the option tag. 正如您已经注意到的那样,选项标签内不允许使用<span> Check here 在这里检查

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

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