我想从以下html div中提取图像src:
<div class="mini-cart-image">
<a href="http://marmot.com/equipment-tents-6-person-tents/midpines-6p/889169900013.html">
<img src="http://s7d2.scene7.com/is/image/marmot/29510_9821_f?$thumb$" alt="Midpines 6P, Orange Spice/Arona, small" title="Midpines 6P, Orange Spice/Arona"/>
</a>
</div>
我使用以下代码从网站上提取了这个div:
var cart_image0 = document.getElementsByClassName("mini-cart-image")[0]
var cart_image0src = cart_image0.getAttribute('src') // null
我想返回src的值。我想过使用getAttribute,但这会返回null。有没有办法用js方法做到这一点?我的另一种方法是手动解析innerHTML以获取src值。我希望有一个更好的解决方案,我不知道。
答案 0 :(得分:4)
尝试var cart_image0 = document.querySelector(".mini-cart-image a img").getAttribute('src');
a
或从选择器中删除var cart_image0 = document.querySelector(".mini-cart-image img").getAttribute('src');
string q2= String.Format("What type of legal entity was the Company during the {0} tax years? ", YourModelName.Years);
答案 1 :(得分:0)
您的代码中发生的是您选择的元素var cart_image0 = document.getElementsByClassName("mini-cart-image")[0]
没有src
属性..
这就是你在调用cart_image0.getAttribute('src')
代码
// Select the cart
var cart = document.getElementsByClassName("mini-cart-image")[0];
// Select the img in the cart
var img = cart.getElementsByTagName('img')[0];
// Get the img src attribute with "img.getAttribute('src')" or shorter "img.src"
console.log(img.src);
&#13;
<div class="mini-cart-image">
<a href="http://marmot.com/equipment-tents-6-person-tents/midpines-6p/889169900013.html">
<img src="http://s7d2.scene7.com/is/image/marmot/29510_9821_f?$thumb$" alt="Midpines 6P, Orange Spice/Arona, small" title="Midpines 6P, Orange Spice/Arona"/>
</a>
</div>
&#13;
请注意,使用cart.getElementsByTagName('img')
,您将获得包含cart
中所有图像的集合。因此,您可以迭代或使用第一个img
元素,如代码示例中所示,与cart.getElementsByTagName('img')[0]
。
答案 2 :(得分:0)
您的问题是func createPDF(on path: String?, from templateURL: URL?, with subject: String?) {
guard let newPDFPath = path,
let pdfURL = templateURL else { return }
let options = [(kCGPDFContextSubject as String): subject ?? ""] as CFDictionary
UIGraphicsBeginPDFContextToFile(newPDFPath, .zero, options as? [AnyHashable : Any])
let templateDocument = CGPDFDocument(pdfURL as CFURL)
let pageCount = templateDocument?.numberOfPages ?? 0
for i in 1...pageCount {
//get bounds of template page
if let templatePage = templateDocument?.page(at: i) {
let templatePageBounds = templatePage.getBoxRect(.cropBox)
//create empty page with corresponding bounds in new document
UIGraphicsBeginPDFPageWithInfo(templatePageBounds, nil)
let context = UIGraphicsGetCurrentContext()
//flip context due to different origins
context?.translateBy(x: 0.0, y: templatePageBounds.height)
context?.scaleBy(x: 1.0, y: -1.0)
//copy content of template page on the corresponding page in new file
context?.drawPDFPage(templatePage)
//flip context back
context?.translateBy(x: 0.0, y: templatePageBounds.height)
context?.scaleBy(x: 1.0, y: -1.0)
// -->>>> Do your change here <<<<--
/* Here you can do any drawings */
}
}
UIGraphicsEndPDFContext()
}
未引用function showhide(show) {
var billingaddress = document.getElementById("billingaddress");
if (show) {
billingaddress.style.display = "block";
} else {
billingaddress.style.display = "none";
}
}
function init () {
var billingcheckbox = document.getElementById("billing");
if (billingcheckbox.checked) {
showhide(true);
} else {
showhide(false);
}
}
元素。
试试这个:
var cart_image0 = document.getElementsByClassName("mini-cart-image")[0]