所以我不知道這是否可能,但我想檢查一個window.OneTrust物件,如果它不存在,將屬性保留為src,否則保留為data-src并更改型別。
這就是我要說的:
如果window.OneTrust存在,請將其保留為:
<script
type='text/plain'
id='google-maps-api-script'
className='optanon-category-C0004'
data-src="https://maps.googleapis.com/maps/api/js?key=<?php echo get_field('google_maps_api_key', 'option') ?>&v=weekly"
></script>;
否則,將其設為:
<script
type='text/javascript'
id='google-maps-api-script'
className='optanon-category-C0004'
src="https://maps.googleapis.com/maps/api/js?key=<?php echo get_field('google_maps_api_key', 'option') ?>&v=weekly"
></script>;
有沒有辦法讓我可以為 type/src 使用三元運算子而不是這樣做?
if (!window.OneTrust) {
<script
type='text/javascript'
id='google-maps-api-script'
className='optanon-category-C0004'
src="https://maps.googleapis.com/maps/api/js?key=<?php echo get_field('google_maps_api_key', 'option') ?>&v=weekly"
></script>;
} else {
<script
type='text/plain'
id='google-maps-api-script'
className='optanon-category-C0004'
data-src="https://maps.googleapis.com/maps/api/js?key=<?php echo get_field('google_maps_api_key', 'option') ?>&v=weekly"
></script>;
}
uj5u.com熱心網友回復:
您可以document.write在<script>元素中使用。
<script>
if (!window.OneTrust) {
document.write(`<script
type='text/javascript'
id='google-maps-api-script'
class='optanon-category-C0004'
src="https://maps.googleapis.com/maps/api/js?key=<?php echo get_field('google_maps_api_key', 'option') ?>&v=weekly"
><\/script>`);
} else {
document.write(`<script
type='text/plain'
id='google-maps-api-script'
class='optanon-category-C0004'
data-src="https://maps.googleapis.com/maps/api/js?key=<?php echo get_field('google_maps_api_key', 'option') ?>&v=weekly"
><\/script>`);
}
</script>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/371625.html
標籤:javascript html 查询
上一篇:陣列結果列在一個下面?
