我试图从一个href属性中获取一个url并追加并返回它,但我一直得到一个“无法读取属性'href'的null”。
我尝试使用document.getElementsByClassName(“CLASS”)。href获取值但返回未定义的值,然后当我使用id时,我得到上面的null错误。
这是我的HTML:
<a class="js-link-name" id="js-link" href="{{ route('report.file.list')}}">Click Me</a>
@push('scripts')
<script>
$(function () {
var questionnaires = [];
@foreach($vm->getQuestionnaires() as $questionnaire)
questionnaires.push(new window.app.locationReports.Questionnaire('{{ $questionnaire->getValue() }}', '{{ $questionnaire->forDisplay() }}'));
@endforeach
new window.app.locationReports.LocationTiles($('.js-location-tiles'), $('.js-file-download-link'), $('.js-legend-trigger'), '{{ $vm->getUserViewPreferenceValue() }}', questionnaires, '{{ route('api.v1.reports.customer-satisfaction.recommend-to-others') }}', '{{ route('report.customer-satisfaction.locations.data.view-preference') }}');
});
</script>
@endpush
这是我的JS功能:
buildLocationReportListUrl(){
let newUrl = URI(document.getElementById('js-link').href).addSearch(this.locationId);
return newUrl;
}
您的代码不完整。但是,当我尝试使用某些修改功能时,它会返回URL。
function buildLocationReportListUrl(){
let newUrl = document.getElementById('js-link').href;
return newUrl;
}
//执行函数返回URL
buildLocationReportListUrl();
希望这可以帮助! :)