0

我试了几次,用不同的方法。我想检查 if 语句的第一个值是否是 iframe。谁能向我解释我在这里做错了什么?我真的很困惑。我需要检查该值是否为 iframe,因此如果该值不是,我可以使用一些 css 隐藏 div 我尝试了几种不同的变体,但我没有找到正确的语法来检查我是否正确。目前该值始终是 iframe,但下一步将是集成或提供 1x1 像素。这就是为什么我必须检查它

const containerToProof = useRef()
useEffect(()=>{
   // console.log('saysomething',containerToProof.current.firstElementChild.innerHTML)   
var isiframe = containerToProof.current.firstElementChild.innerHTML;
console.log('isiframe', isiframe);
    if(isiframe.tagName=== iframe){
        console.log('allo')
        setAdSpotvisible(true)

    }else{

        setAdSpotvisible(false)
    }

 },[<iframe/>]);
/*
if(containerToProof.current=='iframe'){
       console.log('allo')
}*/
    return (
        <div>
           <div  ref={containerToProof}  ></div>



    );`````

4

1 回答 1

0

您可以使用正则表达式进行检查,至少它在我的情况下有效。

您可以执行以下操作:

这是一个工作示例:

const value = "<iframe></iframe>";
const iframeCheck = /<iframe(?: [^>]*?)?(?:\/>|>.*?<\/iframe>)/i;

if (iframeCheck.test(value)) {
  alert('It is an iframe')
} else {
  alert('It is not an iframe')
}

于 2021-04-24T13:39:15.913 回答