在iframe中获取li数据属性可以通过以下步骤实现:
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
var iframeWindow = document.getElementById("myIframe").contentWindow;
或者使用jQuery选择器:
var iframeWindow = $("#myIframe").contents();
使用JavaScript:
var liElement = iframeWindow.document.getElementsByClassName("list-item")[0];
var dataAttribute = liElement.getAttribute("data-attribute");
使用jQuery:
var liElement = iframeWindow.$(".list-item");
var dataAttribute = liElement.data("attribute");
需要注意的是,以上代码仅适用于在同源的情况下获取iframe中的数据属性。如果iframe与父页面不同源,涉及到跨域问题,需要进行额外的处理,例如使用postMessage进行跨域通信。
推荐的腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云