我正在尝试获取所有span标记的HTML,但我只能获取内部HTML。这是我的代码:
var spans = document.getElementsByTagName('span');
for(var i=0;i<spans.length;i++) {
alert($(spans[i]).html());
}
这仅打印标签内的文本,而不是标签。如何用标签打印?
答案 0 :(得分:1)
var spans = $( "span" );
spans.each(function(index,element){
alert(element.outerHTML);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<span>Hallo</span>
&#13;
您可以使用outerHTML和jQuery。希望它有所帮助
答案 1 :(得分:0)
有两种方法可以抓取页面上的所有span元素。
原生Javascript
var spans = document.getElementsByTagName('span');
for (var i in spans){
//print out the whole tag
console.log(spans[0].outerHTML);
}
的jQuery
var spans = $('span');
spans.each(function(index,elem){
//print out the whole tag
console.log(elem.outerHTML);
});
而不是使用烦人的警报,使用console.log(spans);并打开您的Web浏览器控制台以查看打印出来的内容。
编辑:第一张海报是正确的,外部HTML是要走的路。
答案 2 :(得分:0)
04-08 22:47:18.990 1019-1019/system_process E/BatteryService: usbOnlinePath not found
04-08 22:47:18.990 1019-1019/system_process E/BatteryService: batteryVoltagePath not found
04-08 22:47:18.990 1019-1019/system_process E/BatteryService: batteryTemperaturePath not found
04-08 22:47:19.000 1019-1032/system_process E/PowerManagerService-JNI: Couldn't load power module (No such file or directory)
04-08 22:47:19.789 796-796/? E/WVMExtractor: Failed to open libwvm.so
04-08 22:47:19.789 796-796/? E/Trace: error opening trace file: No such file or directory (2)
04-08 22:47:19.800 1019-1056/system_process E/EventHub: could not get driver version for /dev/input/mouse0, Not a typewriter
04-08 22:47:19.800 1019-1056/system_process E/EventHub: could not get driver version for /dev/input/mice, Not a typewriter
04-08 22:47:19.800 1019-1056/system_process E/Trace: error opening trace file: No such file or directory (2)
04-08 22:47:19.830 796-968/? E/WVMExtractor: Failed to open libwvm.so
04-08 22:47:19.870 789-1014/? E/CommandListener: Failed to open /proc/sys/net/ipv6/conf/wlan0/disable_ipv6: No such file or directory
04-08 22:47:19.870 1019-1069/system_process E/WifiStateMachine: Failed to disable IPv6: java.lang.IllegalStateException: command '1 interface ipv6 wlan0 disable' failed with '400 1 Failed to change IPv6 state (No such file or directory)'
04-08 22:47:19.904 1019-1032/system_process E/MobileDataStateTracker: default: Ignoring feature request because could not acquire PhoneService
04-08 22:47:19.904 1019-1032/system_process E/MobileDataStateTracker: default: Could not enable APN type "default"
04-08 22:47:19.990 1081-1081/com.android.systemui E/Trace: error opening trace file: No such file or directory (2)
04-08 22:47:20.110 1097-1097/com.android.inputmethod.latin E/Trace: error opening trace file: No such file or directory (2)
04-08 22:47:20.180 1143-1143/com.android.launcher E/Trace: error opening trace file: No such file or directory (2)
04-08 22:47:20.180 1131-1131/com.android.phone E/Trace: error opening trace file: No such file or directory (2)
04-08 22:47:20.210 1117-1117/com.android.settings E/Trace: error opening trace file: No such file or directory (2)
04-08 22:47:20.290 1167-1167/android.process.acore E/Trace: error opening trace file: No such file or directory (2)
04-08 22:47:20.390 1097-1183/com.android.inputmethod.latin E/ActivityThread: Failed to find provider info for com.android.inputmethod.latin.dictionarypack
04-08 22:47:20.470 1019-1114/system_process E/ThrottleService: problem during onPollAlarm: java.lang.IllegalStateException: problem parsing stats: java.io.FileNotFoundException: /proc/net/xt_qtaguid/iface_stat_all: open failed: ENOENT (No such file or directory)
04-08 22:47:20.540 1097-1183/com.android.inputmethod.latin E/BinaryDictionaryGetter: Could not find a dictionary pack
04-08 22:47:23.615 1225-1225/com.android.deskclock E/Trace: error opening trace file: No such file or directory (2)
04-08 22:47:23.697 1241-1241/com.android.calendar E/Trace: error opening trace file: No such file or directory (2)
04-08 22:47:23.786 1258-1258/com.android.providers.calendar E/Trace: error opening trace file: No such file or directory (2)
04-08 22:47:24.266 1278-1278/com.android.contacts E/Trace: error opening trace file: No such file or directory (2)
04-08 22:47:24.567 796-796/? E/WVMExtractor: Failed to open libwvm.so
04-08 22:47:24.676 796-968/? E/WVMExtractor: Failed to open libwvm.so
04-08 22:47:24.795 1312-1312/android.process.media E/Trace: error opening trace file: No such file or directory (2)
04-08 22:47:24.867 796-796/? E/WVMExtractor: Failed to open libwvm.so
04-08 22:47:25.037 796-968/? E/WVMExtractor: Failed to open libwvm.so
04-08 22:47:25.116 796-796/? E/WVMExtractor: Failed to open libwvm.so
04-08 22:47:25.616 1344-1344/com.android.email E/Trace: error opening trace file: No such file or directory (2)
04-08 22:47:25.715 1377-1377/com.android.exchange E/Trace: error opening trace file: No such file or directory (2)
04-08 22:47:25.887 1406-1406/com.android.mms E/Trace: error opening trace file: No such file or directory (2)
04-08 22:48:35.267 800-800/? E/installd: eof
04-08 22:48:35.267 800-800/? E/installd: failed to read size
04-08 22:48:35.297 1491-1495/? E/Trace: error opening trace file: No such file or directory (2)
04-08 22:48:35.327 1491-1495/? E/EGL_emulation: tid 1495: init(270): error 0x3003 (EGL_BAD_ALLOC)
04-08 22:48:35.327 1491-1495/? E/EGL_emulation: tid 1495: eglCreateWindowSurface(638): error 0x3003 (EGL_BAD_ALLOC)
01-01 00:00:00.000 0-0/? E/Internal: device 'emulator-5554' not found
刚刚添加了一些硬编码的书挡&#39;标题为&#39; span&#39;您可以根据需要修改它们。
它们充当您的html的包装器并在循环的每次迭代中运行。
答案 3 :(得分:0)
以下是另一种方法:
var spans = $('span');
spans.each(function(index,element){
alert( $('<div/>').html(element).html() );
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<span>Hallo</span>
&#13;